繁体   English   中英

路由错误,以表格形式采取措施

[英]Routing Error, get action in a form

这是我的第一个自定义路线,我们将不胜感激。

访问/ quotes / new时的错误

No route matches {:controller=>"QuotesController", :action=>"getdata"}

我的表格:

<%= f.input :make, collection: clothing_types, label: 'Thread Type', remote: true,
 :'data-url' => url_for( :controller => 'QuotesController', :action => 'getdata'), 
 :'data-type' => 'json', input_html: {class: "select_make"} %>

(我尝试将'QuotesController'更改为'Quotes'或'quotes_controller',他们只是更改了错误中的措辞)

Routes.rb

get 'quotes/getdata' => 'quotes#new'

quotes_controller.rb

def getdata
  @make = params[:make]
  @clothes = Product.where(:item_make => @make).all
  render :json => @clothes.map{|c| [c.id, c.name]}
end

当我跑耙路线时,我看到

quotes_getdata GET    /quotes/getdata(.:format)       quotes#new

尝试将get 'quotes/getdata' => 'quotes#new'更改为get 'quotes/getdata' => 'quotes#getdata' 并以url_for( :controller => 'QuotesController', :action => 'getdata')url_for( :controller => 'quotes', :action => 'getdata')

另外,您可以通过这样声明named_route来改善视图代码:

'quotes/getdata' => 'quotes#new', as: :quotes_getdata

以及形式:

<%= f.input :make, collection: clothing_types, label: 'Thread Type', remote: true,
:'data-url' => quotes_getdata_path, :'data-type' => 'json', input_html: {class: "select_make"} %>

您的路线指导quotes/getdatanew的动作QuotesController 当你调用url_for ,它没有找到一个途径,映射到getdata的作用QuotesController

您所需要做的就是修改url_for参数,如下所示:

url_for(controller: 'quotes', action: 'new')

上面将根据需要生成http://.../quotes/getdata

或者,您可以调用quotes_getdata_url代替url_for

两件事情:

url_for(controller: :quotes, action: :getdata)

更重要的是:

该表格是通过GET发送的吗? -如果没有,您应该采用您的路由来响应POST请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM