views:

55

answers:

2

I'm working with Ruby on rails 2.3.4 and I'd like to have a link that executes an action when clicked.

The relevant part of the routes.rb file looks like this:

map.search_filter_relevance "/anuncios/buscar", :controller => 'announcements', :action => 'search_filter_relevance'

My view(it's the model's index page) looks like this:

<%=  link_to 'M&Aacute;S RELEVANTES', search_filter_relevance_path %>

And my controller looks like this:

  def search_filter_relevance
    raise params.inspect
    unless params[:announcements].nil? or params[:announcements].empty?
      @announcements = params[:announcements].order_by_featured

    end
  end

The problem is that when I click the link I get an error due to some null value in the Show action! I'm not accessing that action at all...why is executing it?

EDIT:

Here is the routes output:

search_filter_relevance_announcements GET    /anuncios/search_filter_relevance(.:format)        {:controller=>"announcements", :action=>"search_filter_relevance"}
                        announcements GET    /anuncios(.:format)                                {:controller=>"announcements", :action=>"index"}
                                      POST   /anuncios(.:format)                                {:controller=>"announcements", :action=>"create"}
                     new_announcement GET    /anuncios/new(.:format)                            {:controller=>"announcements", :action=>"new"}
                    edit_announcement GET    /anuncios/:id/edit(.:format)                       {:controller=>"announcements", :action=>"edit"}
                         announcement GET    /anuncios/:id(.:format)                            {:controller=>"announcements", :action=>"show"}
                                      PUT    /anuncios/:id(.:format)                            {:controller=>"announcements", :action=>"update"}
                                      DELETE /anuncios/:id(.:format)                            {:controller=>"announcements", :action=>"destroy"}
                               search        /anuncios/buscar                                   {:controller=>"announcements", :action=>"search"}
                         power_search        /anuncios/buscar                                   {:controller=>"announcements", :action=>"power_search"}
A: 

When you go into console and

rake routes

can you paste all the lines that have search_filter_relevance as well as anuncios ?

Updated: Since you want to call the search_filter_relevance action in the announcements controller you will need to use the listed route search_filter_relevance_announcements_path

<%=  link_to 'M&Aacute;S RELEVANTES', search_filter_relevance_announcements_path %>

One other option would be to specify the controller and action manually

<%=  link_to 'M&Aacute;S RELEVANTES', {:controller => "announcements", :action => "search_filter_relevance"} %>
ThinkBohemian
I edited the original question including this info.
Brian Roisentul
Updated with the rest of my answer.
ThinkBohemian
This is not solving the problem. I guess that due to the order in routes file, it's accesing "search" action instead of the one I want. I can't understand why.
Brian Roisentul
So you tried the two snippets of code and it didn't work? If so, what happened, did you get an error... ?
ThinkBohemian
Nothing. It just keep accessing "search" action instead the one I want. I don't get any erros. It just does the searching again.
Brian Roisentul
Can you post a the action from your log showing the whole request from beginning to end? (you can copy directly from your script/server window if you prefer)
ThinkBohemian
+1  A: 

Try this code in your routes.rb file

 map.resources :announcements,:collection=>{:search_filter_relevance=>:get}

And, comment the below line of code in your routes.rb file

map.search_filter_relevance "/anuncios/buscar", :controller => 'announcements', :action => 'search_filter_relevance'
Vamsi
If I write that I receive an error in my view due to search_filter_relevance_path does not exist anymore. What should I write there instead?
Brian Roisentul
The funny thing is that if I change its path at routes it will call the action I want, but I don't want to create another view for this. I just want to click a link button and sort a table in the action that's called. I don't want to go to another url than 'anuncios/buscar'(the search's url). Any ideas?
Brian Roisentul
1)you do this with the rjs and re-render the table with the help of partial and you can reuse the partial in search view as well. 2)Did you tried :render => index in your search_filter_relevance option.
Vamsi