Rails controller path not found

2019-08-03 18:05发布

问题:

My form tag in my view:

<%= form_tag view_all_rater_path, :method => 'get' do %>
  <p>
    <%= text_field_tag :search, params[:search], :placeholder => 'Search by Set # or date' %></br>
    <%= submit_tag "Search", :class => "btn btn-link", :name => nil %>
  </p>
<% end %>

My controller action:

  def view_all
    if params[:search]
      @ratings = RatingSet.find(:all, :conditions => ['id = ? or rating_date like ?', "%#{params[:search]}%"])
    else
      @ratings = RatingSet.all
    end
  end

My routes:

  resources :rater, :only => [:index] do
    collection do
      get :rater_csv
      get :view_all
    end
  end

When I navigate to /rater/view_all I get a No route matches {:action=>"view_all", :controller=>"rater"}

回答1:

The problem here is going to be singular vs. plurals in your route definitions.

Your routes gives the following output for rake routes:

rater_csv_rater_index GET    /rater/rater_csv(.:format)                                  rater#rater_csv
 view_all_rater_index GET    /rater/view_all(.:format)                                   rater#view_all
          rater_index GET    /rater(.:format)                                            rater#index

Because you've defined a plural resource (resources) with a singular name (rater).

If you make it a singular resource (resource) the routes will clear themselves up.

And always always remember to use rake routes!



回答2:

Did you try "rake routes" on the console to see the structure of your routes?



回答3:

2 everybody: Make sure that you are calling right request, GET or POST as needed.