Rails3:视图和模型之间的简单路由问题

时间:2010-12-10 17:14:09

标签: ruby-on-rails routes

相关代码

的routes.rb

Waterloop3::Application.routes.draw do
   resources :submissions
   match ':controller(/:action(/:id(.:format)))'
end

submission_controller.rb - 包含一个名为sort_by_category_academic的动作 - 列出所有提交的类别,学术。

class SubmissionsController < ApplicationController
  def sort_by_category_academic
    @submissions = Submission.find(:all, 
    :conditions=>["category = academic" ])
  end
end

index.html.erb - 点击链接调用“学术”将用户带到“提交/ sort_by_category_academic”

<%= link_to 'Academic', :controller => "submission", :action => "sort_by_category_academic" %>

值得注意的一些奇怪行为。

在routes.rb中,移动“匹配”:控制器(/:action(/:id(。:format)))“”以上“资源:提交”如下所示。

Waterloop3::Application.routes.draw do
   match ':controller(/:action(/:id(.:format)))' 
   resources :submissions
end

当我这样做的时候,至少提交的内容完全符合学术要求。 但是,现在提交页面都在进行中。我无法通过此更改添加任何新条目。 问题似乎与路由优先级有关,我不太了解。 如果有人知道解决这个问题的原因,我很乐意听到解释。 对我来说,RoR仍然非常严重。

我的问题

我认为这可以胜任。

match ':controller(/:action(/:id(.:format)))'

但是当我点击学术链接时,我一直收到这个错误。

路由错误

No route matches "/submission/sort_by_category_academic"

知道如何解决这个问题吗?


修改

在submissions_controller.rb

中查找操作
  def find
    @submissions = Submission.find(:all, 
    :conditions=>["title = ? OR description = ?", params[:search_string], params[:search_string]])
  end

在index.html.erb中查找文本字段

<%form_tag "/submissions/find" do%>
<%=text_field_tag :search_string%>
<%=submit_tag "Search"%>
<%end%>

因此,假设FIND接受search_string并浏览所有与标题或描述匹配的提交。

3 个答案:

答案 0 :(得分:0)

resources :submissions do
    collection do 
        get :sort_by_category_academic
    end
 end 

您可以删除底部的默认路线

- 编辑----

sort_by_category_academic_submissions GET    /submissions/sort_by_category_academic(.:format)

应该产生

你应该致电

<%= link_to('Academic', sort_by_category_academic_submissions_path) %>

你还有顶部的默认匹配..

我看起来像这样:订单很重要......

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"}

有了这条路线..

WaterLoop::Application.routes.draw do
  resources :submissions do
    collection do 
      get :sort_by_category_academic
    end
  end 
end

答案 1 :(得分:0)

ruby​​ 1.9.2p0(2010-08-18)[i386-mingw32]

C:\ Rails&gt; cd waterloop3

C:\ Rails \ Waterloop3&gt;佣金路线 (在C:/ Rails / Waterloop3中)      提交GET /submissions(.:format){:action =&gt;“index”,:controller =&gt;“提交”}                  POST /submissions(.:format){:action =&gt;“create”,:controller =&gt;“提交”}   new_submission GET /submissions/new(.:format){:action =&gt;“new”,:controller =&gt;“提交”}  edit_submission GET /submissions/:id/edit(.:format){:action =&gt;“edit”,:controller =&gt;“提交”}       提交GET /submissions/:id(.:format){:action =&gt;“show”,:controller =&gt;“提交”}                  PUT /submissions/:id(.:format){:action =&gt;“update”,:controller =&gt;“提交”}                  DELETE /submissions/:id(.:format){:action =&gt;“destroy”,:controller =&gt;“提交”} submissions_find GET /submissions/find(.:format) {:controller =&gt;“提交”,:action =&gt;“find”}

         GET /submissions/sort_by_category_academic(.:format) {:controller =&gt;“提交”,:action =&gt;“academic”}

C:\滑轨\ Waterloop3&GT;

答案 2 :(得分:0)

的routes.rb

Waterloop3 :: Application.routes.draw做

资源:提交   得到“提交/发现”   得到“提交/ sort_by_category_academic(。:格式)”