Ransack 未定义方法 _path Rails 4.2.1 [英] Ransack Undefined Method _path Rails 4.2.1
问题描述
我正在开发一个非常简单的 CRUD 资产管理应用程序,我想使用 Ransack.我想我在我的视图和控制器中正确设置了这个设置,但是每次点击索引视图时我都会收到异常:
未定义的方法
assets_path' for #<#:0x007fa5f353e9e0>`
这是我的观点:
index.html.erb
<%= search_form_for @q do |f|%><%= f.search_field :asset_name_cont, placeholder: 'Search...' %><%结束%><table class="table table-striped"><头><tr><th>资产编号</th><th>资产名称</th>第<第>个序列号第>个<th>模型</th><th>制造商</th><th>资产类型</th><th>动作</th></tr></thead><% @assets.each 做 |a|%><tr><td><%= a.asset_number %></td><td><%= a.asset_name %></td><td><%= a.asset_serial %></td><td><%= a.asset_model %></td><td><%= a.asset_manuf %></td><td><%= a.asset_type.try(:name) %></td><td><%= link_to 'View', inventory_path(a), :class =>'btn btn-mini btn-info'%><%= link_to 'Edit', edit_inventory_path(a), :class =>'btn btn-mini btn-warning'%></td></tr></tbody><%结束%><%= will_paginate @assets %>这是我的控制器摘录:inventory_controller.rb
定义索引@q = Asset.ransack(params[:q])@assets = @q.result.order("asset_number ASC").paginate(:page => params[:page], :per_page => 10)结尾
这是我的模型(带有字段注释)
asset.rb
id :整数不为空,主键资产编号:字符串资产短名称:字符串资产名称:字符串资产描述:文本资产序列:字符串资产模型:字符串资产制造:字符串asset_type_id :整数created_at : 日期时间不为空更新时间:日期时间不为空资产位置:字符串类资产真的归属于:资产类型结尾
我认为我的控制器连接良好,我之前在表单和路由方面遇到了问题,因为有一个名为 assets
的控制器,这是 Rails 4.xx 中作为保留名称的问题,所以我将控制器重建为 inventory
并从内部调用 Asset
类.
我的问题是,我使用 search_form_for
字段查看 Asset
模型的 asset_name
模型字段,但是当我编写视图时已经布置好了,我经常得到那个路径错误.
有没有办法将不同的路径传递到 Ransack search_field
方法中,以便我可以解决似乎是约定问题的问题?
如果您需要更多信息或者我的问题不够清楚,请随时编辑或询问更多信息.谢谢大家!
解决方案 好的,我想通了.与我在其他表单中看到的违反 Rails 命名约定的问题类似,我必须在 search_form_for
辅助方法中传递 url 和方法.它正在工作!
这是我现在对搜索的看法:
<%= search_form_for(@q, url: "/inventory", method: :get) do |f|%><%= f.search_field :asset_name_cont, placeholder: 'Search...' %><%结束%>
I'm working on a very simple CRUD Asset Management app and I want to use Ransack. I think I have this setup properly in my view and controller but each time the index view is hit I get the exception:
undefined method
assets_path' for #<#:0x007fa5f353e9e0>`
Here is my view:
index.html.erb
<div class="pull-right">
<%= search_form_for @q do |f| %>
<%= f.search_field :asset_name_cont, placeholder: 'Search...' %>
<% end %>
<table class="table table-striped">
<thead>
<tr>
<th>Asset Number</th>
<th>Asset Name</th>
<th>Serial Number</th>
<th>Model</th>
<th>Manufacturer</th>
<th>Asset Type</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @assets.each do |a| %>
<tr>
<td><%= a.asset_number %></td>
<td><%= a.asset_name %></td>
<td><%= a.asset_serial %></td>
<td><%= a.asset_model %></td>
<td><%= a.asset_manuf %></td>
<td><%= a.asset_type.try(:name) %></td>
<td><%= link_to 'View', inventory_path(a), :class => 'btn btn-mini btn-info' %> <%= link_to 'Edit', edit_inventory_path(a), :class => 'btn btn-mini btn-warning' %></td>
</tr>
</tbody>
<% end %>
<%= will_paginate @assets %>
Here is my controller excerpt:
inventory_controller.rb
def index
@q = Asset.ransack(params[:q])
@assets = @q.result.order("asset_number ASC").paginate(:page => params[:page], :per_page => 10)
end
And here is my model (with annotation of the fields)
asset.rb
id :integer not null, primary key
asset_number :string
asset_shortname :string
asset_name :string
asset_desc :text
asset_serial :string
asset_model :string
asset_manuf :string
asset_type_id :integer
created_at :datetime not null
updated_at :datetime not null
asset_location :string
class Asset < ActiveRecord::Base
extend FriendlyId
friendly_id :asset_name, use: :slugged
validates :asset_name, :asset_serial, :asset_type_id, :asset_model, :asset_manuf, :presence => true
belongs_to :asset_type
end
I think I have the controller wired up fine, I was having issues before with forms and routes by having a controller called assets
which is an issue as a reserved name in Rails 4.x.x so I rebuilt the controller as inventory
and call the Asset
class from within.
My question is, I the search_form_for
field to look at the Asset
model's field for asset_name
but when I write the view as I've laid out I constantly get that path error.
Is there a way to pass a different path into the Ransack search_field
method so that I can get past what seems to be an issue with conventions?
If you need more information or if my question is not clear enough, please do not hesitate to edit or ask for more information. Thanks all!
解决方案 Ok, I figured this out. Similar to issues I was seeing with breaking Rails naming conventions in my other forms, I had to pass the url and method in the search_form_for
helper method. It's working!
Here's my view for the search now:
<%= search_form_for(@q, url: "/inventory", method: :get) do |f| %>
<%= f.search_field :asset_name_cont, placeholder: 'Search...' %>
<% end %>
这篇关于Ransack 未定义方法 _path Rails 4.2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆