New actions on command

Hello!

I'm developing a classical Blog like app, and I'm having some kind of issues about posts' tags, because I've correctly installed acts_as_taggable and acts_as_taggable_on_steroids gems but none of them work, for a controller's mismatch error (due to a Post's tags:string attribute I guess), but still, this is not important.

What I want to do is a Blogspot's like schema, so basically there is the http://website/search/label/***TAG_NAME*** url, which contains all the posts done with this specific tag. Well then, I've split the tags and each tag redirect to the correct url, which contains all the posts made with the related tag, BUT I just manually created the routes and templates.

The problem is : how could I tell to the controller to create an action, or a method with the same name of those tags inside the Search controller? I was thinking to a Proc, but I have no clue about how to structure it. Could you help me?

And also, when I create a new Post, if I write in tags' text_field something like "wood,fire", Rails will save the string as a single tag, but what I evidently want is to .split(',') this string. Yet, I'm not able to find the place in between f.submit and the PostsController, where the tags would be sent to the Controller already split up. Cos, if I put the split after the :

if @post.save      @post.tags.split(',')

...in the console I get:

Post.last

=> [ #etc etc. tags : "wood,fire"]

and it isn't split. Otherwise, if I put the .split(',') method somewhere into the _form I get errors.

So, basically I'm looking for the "place" in the middle between the submit action and the Controller's final check. Any idea??? :slight_smile:

Hello!

I’m developing a classical Blog like app, and I’m having some kind of

issues about posts’ tags, because I’ve correctly installed

acts_as_taggable and acts_as_taggable_on_steroids gems but none of them

work, for a controller’s mismatch error (due to a Post’s tags:string

attribute I guess), but still, this is not important.

What I want to do is a Blogspot’s like schema, so basically there is the

http://website/search/label/TAG_NAME url, which contains all the

posts done with this specific tag.

Well then, I’ve split the tags and each tag redirect to the correct url,

which contains all the posts made with the related tag, BUT I just

manually created the routes and templates.

The problem is : how could I tell to the controller to create an action,

or a method with the same name of those tags inside the Search

controller? I was thinking to a Proc, but I have no clue about how to

structure it. Could you help me?

And also, when I create a new Post, if I write in tags’ text_field

something like “wood,fire”, Rails will save the string as a single tag,

but what I evidently want is to .split(‘,’) this string.

Yet, I’m not able to find the place in between f.submit and the

PostsController, where the tags would be sent to the Controller already

split up.

Cos, if I put the split after the :

if @post.save

 @post.tags.split(',')

…in the console I get:

Post.last

=> [ #etc etc. tags : “wood,fire”]

and it isn’t split. Otherwise, if I put the .split(‘,’) method somewhere

into the _form I get errors.

So, basically I’m looking for the “place” in the middle between the

submit action and the Controller’s final check. Any idea??? :slight_smile:

Look at acts_as_taggable_on.

But it doesn't work on my app.

I don't know why, I downloaded it and installed with bundle install, but still when I add to a certain model the string : acts_as_taggable

I systematically obtain an error, so I was trying to avoid this by creating by myself the method.

But it doesn’t work on my app.

I don’t know why, I downloaded it and installed with bundle install, but

still when I add to a certain model the string :

acts_as_taggable

I systematically obtain an error, so I was trying to avoid this by

creating by myself the method.

there’s actually 3 gems for tagging, you mentioned the first two and I said the other one.

I suggest you uninstall the gems you mentioned and use acts_as_taggable_on

https://github.com/mbleigh/acts-as-taggable-on

If you have troubles making it work, post the errors and we’ll see what we can do to help

debug. Sometimes, it’s nice to try to do some things manually but in this case, doing so

will just cost you time and effort.

you were splitting after you saved the model - you want to do that before - or better use a before save call back on the model to split before the model is stored

I ultimately written the app from the beginning, this time including 'acts_as_taggable_on' from the beginning, and it works! I'm definitely sure that it depended to a name mismatch, since the Post model had already an attribute called :tags . Even by removing this attribute with a "rails g migration remove_tags_from_posts tags:string" the PostsController was puzzled by the name mismatch, and yes, I also removed the attribute from schema.rb ; there is evidently some other file that should be modified as well, but which file is it I couldn't say.

Right now I will try to code the tag-based indexing, and I'll let you know!

Cheers and thank for your help! L

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])

but Rails insists in fetching the id as parameter.

Any idea? Thanks guys!

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

I tried to do as you suggested but it doesn't show anything. I've tried also to figure out if it wasn't a nil object but it's not even this. For instance, on tag.html.erb :

<% @posts.each do |post| %>   <%= post.title.class%>   <%= post.tags%> <%end%>

it gives as result a blank page, it not even shows which class are those elements. But still, it doesn't either give errors.

What I did then was to change the 'tag' action in PostsController in :

    @posts = Post.tagged_with(params[:id])

But then I'm wondering if the mistake isn't in the post's index.html.erb , because this is what I did :

<% @posts.each do |post| %>   <tr>     <td><%= post.title %></td>     <td><%= post.body %></td>     <td> <% post.tag_list %>   <% b = a.to_a %> <% b.each do |tag|%> <%= link_to tag, {:controller => :posts, :action => :tag, :id => tag.id }%> <% end%>

and I did this because otherwise the tag_list would appear as a single string merged together, even if I use a split method. This works, or at least it makes the tags appear singularly, but still, when I go to the url http://0.0.0.0:3000/posts/tag/#TAG_ID\# I get and empty page, even though the ROUTES.RB has this string uncommented:    match ':controller(/:action(/:id(.:format)))'

...no idea :-\

I tried to do as you suggested but it doesn’t show anything. I’ve tried

also to figure out if it wasn’t a nil object but it’s not even this.

For instance, on tag.html.erb :

<% @posts.each do |post| %>

<%= post.title.class%>

<%= post.tags%>

<%end%>

it gives as result a blank page, it not even shows which class are those

elements. But still, it doesn’t either give errors.

What I did then was to change the ‘tag’ action in PostsController in :

@posts = Post.tagged_with(params[:id])

But then I’m wondering if the mistake isn’t in the post’s index.html.erb

, because this is what I did :

<% @posts.each do |post| %>

<td><%= post.title %></td>

<td><%= post.body %></td>

<td> <% post.tag_list %>

<% b = a.to_a %>

<% b.each do |tag|%>

<%= link_to tag, {:controller => :posts, :action => :tag, :id => tag.id

}%>

<% end%>

and I did this because otherwise the tag_list would appear as a single

string merged together, even if I use a split method. This works, or at

least it makes the tags appear singularly, but still, when I go to the

url

http://0.0.0.0:3000/posts/tag/#TAG_ID#

I get and empty page, even though the ROUTES.RB has this string

uncommented:

match ‘:controller(/:action(/:id(.:format)))’

Sorry but I’m a little confused now on what’s happening in your app. Please answer the following questions:

  1. is your view for the index and tag actions the same?

  2. since you are now using params[:id] instead of params[:name], are you getting the right posts?

  3. please post the corresponding routes line that matches /posts/tag/tag_name

1. Nope. There's the main index:     0.0.0.0:3000/posts #which is placed in => /views/posts/index.html.erb

   and there's the tag action:    0.0.0.0:3000/posts/tag/TAG_NAME #which is placed in /views/posts/tag.html.erb

2. Nope. But I didn't get the right post neither before. Changing from (params[:name]) to (params[:id]) didn't change big things. Even though the url 0.0.0.0:3000/posts/tag/TAG_NAME or also 0.0.0.0:3000/posts/tag/TAG_ID are recognized and loaded, they don't display the relatives posts.

3. In routes.rb there's this line that should be sufficient as instruction, but maybe I'm missing something :    resources :posts    match ':controller(/:action(/:id(.:format)))'

Thanks for your help!

  1. Nope. There’s the main index:

    0.0.0.0:3000/posts #which is placed in =>

/views/posts/index.html.erb

and there’s the tag action:

0.0.0.0:3000/posts/tag/TAG_NAME #which is placed in

/views/posts/tag.html.erb

what i’m asking is if they have the same code.

  1. Nope. But I didn’t get the right post neither before. Changing from

(params[:name]) to (params[:id]) didn’t change big things. Even though

the url 0.0.0.0:3000/posts/tag/TAG_NAME or also

0.0.0.0:3000/posts/tag/TAG_ID are recognized and loaded, they don’t

display the relatives posts.

can you put a debugger inside the tag action and paste the output of params.inspect

  1. In routes.rb there’s this line that should be sufficient as

instruction, but maybe I’m missing something :

resources :posts match ‘:controller(/:action(/:id(.:format)))’

so the route that matches the url is the last one, so params[:id] should have the tag name

in the tag action. are you sure you have posts in your database that are properly tagged?

1. No, in index.html.erb there's just the standard template.    in tag.html.erb I've tried to do several ways, none gives an error but none shows the tagged posts either. Right now I tried a simpler solution : a plain <%@posts.each do |post| %> <%= post.tags.class %> <%= post.class %> <%end%>

where @posts is def tag @posts = Post.tagged_with(params[:id]) end

under the PostsController.rb .

but the shown view is just empty, not even NilClass.

2. for this tag.html.erb view: <% debug @posts.each do |post| %>   <%= debug post.title%><br>   <%= debug post.tags %>   <%end%>

I obtain : #<Enumerable::Enumerator:0x104144240>

which is referred to <% debug @posts.each do |post| %>

if I add or delete debug on the others params, nothing changes.

3. Absolutely sure, from the console :

a = Post.first

=> #<Post id: 1, title: "the sea is beautiful", body: "because it's beautiful", created_at: "2011-08-31 14:21:14", updated_at: "2011-08-31 14:21:14">

a.tag_list

=> ["water", "landscape"]

a.tags

=> [#<ActsAsTaggableOn::Tag id: 1, name: "water">, #<ActsAsTaggableOn::Tag id: 2, name: "landscape">]

  1. No, in index.html.erb there’s just the standard template.

    in tag.html.erb I’ve tried to do several ways, none gives an error

but none shows the tagged posts either.

Right now I tried a simpler solution : a plain

<%@posts.each do |post| %>

<%= post.tags.class %>

<%= post.class %>

<%end%>

where @posts is

def tag @posts = Post.tagged_with(params[:id])

end

under the PostsController.rb .

but the shown view is just empty, not even NilClass.

  1. for this tag.html.erb view:

<% debug @posts.each do |post| %>

<%= debug post.title%>

<%= debug post.tags %>

<%end%>

I obtain :

#Enumerable::Enumerator:0x104144240

which is referred to <% debug @posts.each do |post| %>

if I add or delete debug on the others params, nothing changes.

  1. Absolutely sure, from the console :

a = Post.first

=> #<Post id: 1, title: “the sea is beautiful”, body: "because it’s

beautiful", created_at: “2011-08-31 14:21:14”, updated_at: "2011-08-31

14:21:14">

a.tag_list

=> [“water”, “landscape”]

a.tags

=> [#<ActsAsTaggableOn::Tag id: 1, name: “water”>,

#<ActsAsTaggableOn::Tag id: 2, name: “landscape”>]

so doing Post.tagged_with(‘water’) should return the first Post record.

and going to /posts/tag/water gives a blank page? If the answer to this is yes,

give me a copy of your code, I’ll try to run it locally.