Ruby on Rails Thursday, September 1, 2011



On Thu, Sep 1, 2011 at 6:42 PM, Leo M. <lists@ruby-forum.com> wrote:
All right, I just need the last step to go.

I've done everything correctly, so the index show a briefing of the
post, and all the tags, which are correctly linked to a proper url,
specifically :
 let's put I click on the tag "aqua" , the specified url shall be

http://0.0.0.0:3000/posts/tag/aqua

that's just fantastic!
I've added a

def tag
   @post = Post.tagged_with(params[:name])
     respond_to do |format|
     format.html # show.html.erb
     format.xml  { render :xml => @post }
   end
end

in PostsController.
Everything works fine, but when I put the same view/show.html.erb code
to view/tag.html.erb , it tells me that:

the url is correct > http://0.0.0.0:3000/posts/tag/aqua

and the error is:

undefined method `title' for #<ActiveRecord::Relation:0x1041fd4c0>

This happens because it tries to fetch all the posts with ID => "aqua" ,
which don't exist.
I'm puzzled because I specified in the controller that :
@post = Post.tagged_with(params[:name])


Post.tagged_with(params[:name]) returns an ActiveRecord::Relation object not a Post object.
You need to iterate through all the posts tagged with params[:name] so do the following

in the controller method, use

@posts = Post.tagged_with(params[:name]) #note that it's plural

and in tag.html.erb, instead of using @post, use @posts but iterate through each post

<% @posts.each do |post| %>
  <%= post.title %>
<% end %>
 
but Rails insists in fetching the id as parameter.

Any idea?
Thanks guys!

--
Posted via http://www.ruby-forum.com/.

--
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.




--
-------------------------------------------------------------
visit my blog at http://jimlabs.heroku.com

--
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.

No comments:

Post a Comment