Monday, November 8, 2010

[Rails] Re: having trouble accessing a nested resource in rails 2.3.5

I managed to make it work well enough.

I put the following into the blog controller:
def show
@blog = Blog.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @blog }
end
end

then used ajax to load the commends from the show URL into the
index...probably not the sanest way to do it, but i kept getting
errors otherwise.

On Nov 7, 12:56 pm, aperture science <mekkagoj...@gmail.com> wrote:
> I am trying to add comments to my blog.
>
> comments_controller.rb:
>   def create
>     @blog = Blog.find(params[:blog_id])
>     @comment = @blog.comments.create(params[:comment])
>     redirect_to blog_path(@blog)
>   end
>
> In my blog's index view:
>   <% form_for([ @blog, @comment ]) do |f| %>
>     ...
>   <% end %>
>
> blog.rb:
>   has_many :comments
>
> comment.rb:
>   belongs_to :blog
>
> routes.rb:
>   map.resources :blogs, :has_many => :comments
>
> The error I get is:
>   undefined method 'nil_class_path'
> and it points to the form_for line
>
> I can add comments from the console, but if I attempt to view them by
> adding "@comments = Blog.comments.all" to my blog_controller's index
> action, then "<% @comments.each do |comment| %> " into the
> corresponding view, I get an "undefined method 'comments'" error
>
> Any assistance would be awesome.  And I am only using rails 2 because
> my host does not support rails 3. I do not have any trouble getting
> the above to work in rails 3, but i don't know what I am doing wrong
> now.
>
> Thanks.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate