RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I figured it out. I had to restart the server :(

Avatar

I always seem to get this error:

undefined local variable or method `acts_as_tree' for #<Class:0x2421858>

when I add acts_as_tree to the model. Any idea what might be happening?

Avatar

@heldopslippers, I don't recommend acts_as_tree for doing this, instead try awesome_nested_set (mentioned in earlier comments).

@daniel, see the About section of this site, the TextMate theme can be found there. :)

@Michael, good point and thanks for the link!

Avatar

recommended usage : $1.pluralize.classify.constantize

classify only handles pluralized names. so 'bussiness'.classify would yield 'bussines'

Avatar

Jens, you need to run Ruby script/generate nifty_layout before running nifty_scaffold. nifty_layout generates the title helper.

Avatar

What would i need to change to store the session-data in the db?

Avatar

Simple problem:

The following liine through an error.

<!-- user_sessions/new.html.erb -->
<% title "Login" %>

Undefined method. I guess title is a helper method? But nowhere defined.

Any help appreciated
Jens

Avatar

@Ryan Thank you very much.
I used it in many models but this one didn't cut it !!

:proposal has_many :sections, :through=>:proposalsections

I am in create proposal form and have this in it

<% fields_for "proposal[proposalsection_attributes][]" do |proposalsection_form| %>

-A number of checkboxes are supposed to be in it,
-Each is supposed to save an instance of proposalsection if checked before submit!!
[their quantity is fixed for a proposal].

-On submit before putting any checkboxes code it is giving me the error -
 NameError in Proposal#new

Showing app....where line #40 raised:

`@proposal[proposalsection_attributes]' is not allowed as an instance variable name

Extracted source (around line #40):

37: <%= proposal_form.text_area :description, :rows=>7, :cols=>30 %>
38: </p>
39:
40: <% fields_for "proposal[proposalsection_attributes][]" do |proposalsection_form| %>
41:
42:
43: <div>

Can anyone analyze what is it which is Wrong here?

Avatar

Thanks Ryan for all you do.

It this episode I'm having a problem with the stylesheet helper.

I'm trying to put a stylesheet into a partial and it's not being brought into the cascade. I believe this is because the head is rendered before it pulls in the partials.

Is there a work around for this? I'm guessing this wouldn't be the best practice, but I sort of like the idea of being able to have snippets of css loaded only when needed by the partial. Plus it helps clean up my one GIANT application.css file.

Avatar

I'd love to see an Authlogic + acl9 + Cucumber demo!

Avatar

Great episode. I also covered the acts_as_tree plugin for CMS navigation menus. I also went one step further with being able to sort the menu using the acts_as_list:

http://ramblings.gibberishcode.net/archives/one-activerecord-model-acting-as-a-list-and-tree/33

There's some gotchas to watch out for, esp. with moving nodes around in the tree structure.

Avatar

Hello Ryan, thanks for another great video, but my question is about your textmate. What is that theme used in this screencast?

Avatar

I am running on Rails 2.3.2 as well and getting the error where the server responds in HTML instead of javascript. Brad's solutions above worked for me, though. Hoping to see a more elegant solution to this...

Avatar

Acts_as_tree has another major downside. When you delete a node it will delete all its decedents with it. This because the has_many children relationship has the :dependent => :destroy option.

Thus deleting a page will also delete all pages below it. Perhaps the alternative act_as_tree plugins make this behavior configurable (as it should).

Avatar

Ryan,
im trying to follow your example in my rails setup (2.3.2 version) and my task_attributes= setter method doesn't even get invoked. did something change in 2.3.2?

thanks

Avatar

It's a pitty that this function do not protect the public-folder. Do you know a possibility, sans .htaccess?

Avatar

@Ryan, great screencast. Thank you.

@all, I recently made a plugin for creating simple navigations (independent from models). It is well documented and easy to use. Please check it out on github: http://wiki.github.com/andi/simple-navigation

Avatar

I cheat a little when creating polymorphs by using:

./script/generate model comment content:string commentable:references

Still have to go back and add the ":polymorphic => true" parts, but every little bit helps I guess.

Avatar

@Ryan ouch… it is really THAT easy. You just safed me a couple of hours. Thank you very much!

Avatar

I get a $.ajax() error when the plugin calls prototype.js.

Avatar

Hi I am very new to Rails and these Railscast have been awesome.
Just one thing I am hung up on, I am having trouble with my redirect.
You link to "Add Comments" from
/installations/1/onposts/2/ it goes to /onposts/2/comments
The problem is once someone has added a comment I want it to redirect back to the
/installations/1/onposts/2/. Any help would be greatly appreciated.
Thanks

Avatar

Update: looks like this is a bug in rails. Which is/being fixed:

https://rails.lighthouseapp.com/projects/8994/tickets/2022-jserb-templates-broken-in-230-rc1

Avatar

I am also getting the issues reported by many of the comments - where the results are render as HTML instead of JS.
{ render :layout => false } did not solve my issue.
However, I noticed that *.js.erb is always rendered as HTML. Changing it back to *.js.rjs correctly render it as JS.
I am on rails 2.3.2.

Avatar

definitely RA is uglier but at least you can customize what you don't like, and some features of authlogic don't amuse me at all. for instance, the brute force block comes to action if you try a username for more than a limit number of times. in my opinion this should be based on the IP, because 1) someone have more space to try a common password to all usernames and 2) someone can send a lot of requests until all usernames get blocked.

Avatar

@Nils, instead of a content column in the page model, you would add a "url" string column. You would then link to that URL instead of the page's show action.

<%= link_to h(page.name), page.url %>

To determine the current page I suggest a helper method that looks something like this:

def current_page
  Page.find_by_url(request.request_uri)
end

You can then use current_page instead of @page in the view. Of course you may want to make that implementation smarter.

Everything else should be the same.

Avatar

@Fredd, good point. It would be nice if acts_as_tree added these validations for you. Maybe someone has already done this. I haven't looked at the forks.

@José, good question. If you want to nest the elements in the select menu then acts_as_tree is not best, try a nested set instead (see below).

@Ben, @Rafael, @David, thanks for the suggestion on using a nested set. I like the simplicity that acts_as_tree brings, and I think for this case it works well enough because we aren't trying to grab deeply nested child elements in one query.

I plan to do a separate episode on a threaded comment system which uses a nested set.

@Andre, it is definitely possible. You will need to convert the tree into javascript and present it how you want. Sorry I can't go into more detail.

@Jason, it's a secret.

@Geri, @chovy, that is out of the scope of this screencast. But I will consider doing a future episode on the topic. Thanks!

Avatar

I didn't quite get the part with how to deal with controllers and actions… could you give me some more advices on that? or explain it a bit more in detail or so?

Avatar

I wonder what codes is in Project Controller? Can anyone provice me with some cource codes about advanced search!

Avatar

Is anyone can provice me with some source code of Advanced Search? Thank you very much!

Avatar

my company switched what we were using to organize a tree based structure of data to awesome_nested_set, its quicker , easier for doing queries and locating nodes and also has more convenience methods. I highly recommend it (http://github.com/collectiveidea/awesome_nested_set/tree/master). Chef Recommends !

Avatar

Great intro...however, it would be nice to see how to generate the urls with the title of the page.

Typically, for a content-based site, I would rather have /shipping-info instead of /page/1 as the url.

Also, the previous comment about a tree selection menu would be cool to see.

 page 1
    page 1b
    page 1c
 page 2

etc.etc..

Avatar

Great screencast! I´m very interested in how to use the URL in the Page Model for more complex sites with more controllers and actions. Redirect to this URL? I think that´s not a good idea for SEO issues.

Avatar

Nice screencast Ryan. If you or anyone is looking for a quick,easy way to generate ascii trees in the console: http://tagaholic.me/2009/03/18/ruby-class-trees-rails-plugin-trees-with-hirb.html#active_record_trees

Avatar

Awesome episode, Ryan. That's a quick and neat way to generate tree page navigation.

Avatar

Hey Ryan,
great episode. But i have a question. Is it possible to make a dynamic Select Menu with acts_as_tree like you do it in Ep.88?

Avatar

Hi Ryan,

I use awesome_nested_set, where I can use the order btw. the pages as well.
May be an even deeper screencast would be cool on how to build a sophisticated navigation within a more complex app.

E.g. you are viewing posts in the forum, then even if you are
in /community/forum/topics/5

There are several plugins around for this also, one of them is this:
http://github.com/rpheath/navigation_helper/tree/master

Also, read this post:
http://www.railslodge.com/plugins/1226-awesome-nested-set

Avatar

Great one, Ryan! There's one more it could be covered which is showing the hierarchy in a collection_select. tree_select is good but not perfect.

do you have any thoughts on that subject? Thanks! Keep it up!

Avatar

V: Since version 0.3.3 -n is an alias for --name, and it expects an argument. Run cucumber --help. Also, read the History.txt file.

Avatar

You really should look into better_nested_set (http://github.com/chris/better_nested_set/tree/master). Performance-wise its way better then acts_as_tree.

Avatar

I was going to ask the same question!

Avatar

Nice, episode. I've been using acts_as_tree ever since I started with ROR. One thing I noticed though: What happens if you edit a page so that one of its sub-pages becomes its parent? Wouldn't the whole structure break? Would definitely be great with some validation that takes care of that.

Avatar

shouldn't the 'user_sessions' be 'user_session' and the route 'resources' be 'resource'?

Avatar

Hey Ryan,

Thanks for another great episode, but i'm having a couple if issues with Authlogic and nifty-generators. I followed your instructions on how to install nifty-generators and authlogic, but everytime i try to run nifty_scaffold i get this.

Missing these required gems:
  authlogic

You're running:
  ruby 1.8.6.114 at /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
  rubygems 1.3.1 at /Users/john/.gem/ruby/1.8, /Library/Ruby/Gems/1.8, /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8

Run `rake gems:install` to install the missing gems.

i've run rake gems:install and still nothing. when i comment out config.gem 'authlogic' and run it then i get:

Couldn't find 'nifty_scaffold' generator

I'm guessing it's a $PATH issue. i have gem in /usr/local/bin and everything else (rails, rake, etc) in /usr/bin

any ideas?

Thanks,
John

Avatar

If you are new and following along at home with a newer version of Cucumber (I've got 0.3.5), the -n switch for the "cucumber features" command will generate an error. Apparently -n refers to a different option in later versions, and the current equivalent is "--no-source".

Love these testing screencasts, Ryan! They are the only comprehensive source I've seen that walks through using all of the tools together to write tests for and code for a vaguely realistic Rails app, without assuming I am already a testing wizard.

Avatar

Thanks for the great Railscasts...

I had trouble installing webrat on ubuntu until I installed the required packages described on this page http://wiki.rubyonrails.org/testing/webrat#installation

-- Jean-Marc

Avatar

I got the same error as Alexander.. Had a look at the patch, but don't know how to implement it. Help anyone?

Avatar

Hey, I'm getting "missing argument: -n (OptionParser::MissingArgument)" when I try to run cucumber features -n

Any ideas about this?

Avatar

Is there a way to to emulate an is_a relationship with RoR? i was thinking some combination o STI and polymorphic. I'm trying to do "agenda has_many 'subtasks'".
Task :id :name :index :subtask_type :subtask_id
SubTaskFoo :id
SubTaskBar :id

Avatar

How can I replace submit_tag buttons with link_to or link_to_function?

I want to use multiple link_to_function to replace the submit_tag, to prevent users from accidentally sending a form when press the enter key.

However, I cannot figure out a way to pass :name => 'preview_button' in link_to_function.

This is how I submit a form via link_to_function:

<%= link_to_function('Save', "$('ProductForm').submit()") %>

Please help
Thanks

Avatar

I'm still a bit of a newbie I guess because there are lots of surprises for me in rails development. Anyways wanted to mention that in order to get this to work I had to add a new layout called categories.js.erb, which was empty except for the yield tag. Otherwise when I went to localhost/categories.js the file would get run through the same layout as the html views, which is obviously unwanted. If there's a way around that I would appreciate the update.

thanks for the helpful info Ryan!