Rails3: Simple routing problem between view and model
Relevent codes
routes.rb
Waterloop3::Application.routes.draw do
resources :submissions
match ':controller(/:action(/:id(.:format)))'
end
submission_controller.rb - Contains an action called sort_by_category_academic - Lists all the submissions with a category, academic.
class SubmissionsController < ApplicationController
def sort_by_category_academic
@submissions = Submission.find(:all,
:conditions=>["category = academic" ])
end
end
index.html.erb - Clicking on a link call "Academic" brings the user to "submissions/sort_by_category_academic"
<%= link_to 'Academic', :controller => "submission", :action => "sort_by_category_academic" %>
Some weird behaviours worth noting.
In routes.rb, move " match ':controller(/:action(/:id(.:format)))' " above " resources :submissions " like below.
Waterloop3::Application.routes.draw do
match ':controller(/:action(/:id(.:format)))'
resources :submissions
end
When I did this, at least submission was routed to academic perfectly. However, now the submission page was all acting up. I couldn't put any new entries with this change. The issue seems to have been with routing priority which I don't quite know about. If anyone else knows the reason why this fixed the problem, I would love to hear the explanation. RoR is still quite myserious to me.
My question
I thought this would do the job.
match ':controller(/:action(/:id(.:format)))'
But I keep getting this error when I click on the link, Academic.
Routing Error
No route matches "/submission/sort_by_category_academic"
Any idea how to get around this problem?
EDIT
Find action in submissions_controller.rb
def find
@submissions = Submission.find(:all,
:conditions=>["title = ? OR description = ?", params[:search_str开发者_运维百科ing], params[:search_string]])
end
Find text field in index.html.erb
<%form_tag "/submissions/find" do%>
<%=text_field_tag :search_string%>
<%=submit_tag "Search"%>
<%end%>
So FIND is suppose to take in search_string and go through all the submissions that match title or description.
try
resources :submissions do
collection do
get :sort_by_category_academic
end
end
And you can probably remove the default route at the bottom
--edit----
sort_by_category_academic_submissions GET /submissions/sort_by_category_academic(.:format)
should produce that
the you should just call
<%= link_to('Academic', sort_by_category_academic_submissions_path) %>
Do you still have the default match at the top..
mine looks like this: order is important...
sort_by_category_academic_submissions GET /submissions/sort_by_category_academic(.:format) {:action=>"sort_by_category_academic", :controller=>"submissions"}
submissions GET /submissions(.:format) {:action=>"index", :controller=>"submissions"}
POST /submissions(.:format) {:action=>"create", :controller=>"submissions"}
new_submission GET /submissions/new(.:format) {:action=>"new", :controller=>"submissions"}
edit_submission GET /submissions/:id/edit(.:format) {:action=>"edit", :controller=>"submissions"}
submission GET /submissions/:id(.:format) {:action=>"show", :controller=>"submissions"}
PUT /submissions/:id(.:format) {:action=>"update", :controller=>"submissions"}
DELETE /submissions/:id(.:format) {:action=>"destroy", :controller=>"submissions"}
with this routes..
WaterLoop::Application.routes.draw do
resources :submissions do
collection do
get :sort_by_category_academic
end
end
end
ruby 1.9.2p0 (2010-08-18) [i386-mingw32]
C:\Rails>cd waterloop3
C:\Rails\Waterloop3>rake routes
(in C:/Rails/Waterloop3)
submissions GET /submissions(.:format) {:action=>"index", :controller=>"submissions"}
POST /submissions(.:format) {:action=>"create", :controller=>"submissions"}
new_submission GET /submissions/new(.:format) {:action=>"new", :controller=>"submissions"}
edit_submission GET /submissions/:id/edit(.:format) {:action=>"edit", :controller=>"submissions"}
submission GET /submissions/:id(.:format) {:action=>"show", :controller=>"submissions"}
PUT /submissions/:id(.:format) {:action=>"update", :controller=>"submissions"}
DELETE /submissions/:id(.:format) {:action=>"destroy", :controller=>"submissions"}
submissions_find GET /submissions/find(.:format) {:controller=>"submissions", :action=>"find"}
GET /submissions/sort_by_category_academic(.:format) {:controller=>"submissions", :action=>"academic"}
C:\Rails\Waterloop3>
Routes.rb
Waterloop3::Application.routes.draw do
resources :submissions get "submissions/find" get "submissions/sort_by_category_academic(.:format)"
end
精彩评论