【问题标题】:Ransack Undefined Method _path Rails 4.2.1Ransack未定义方法_path Rails 4.2.1
【发布时间】:2015-06-20 21:23:58
【问题描述】:

我正在开发一个非常简单的 CRUD 资产管理应用程序,我想使用 Ransack。我认为我在视图和控制器中正确设置了此设置,但每次点击索引视图时都会出现异常:

undefined methodassets_path' 用于#`

这是我的看法:

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 %>

这是我的控制器摘录: 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

这是我的模型(带有字段注释)

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

我想我的控制器连接得很好,我之前在表单和路由方面遇到了问题,因为我有一个名为 assets 的控制器,这是 Rails 4.x.x 中的保留名称的问题,所以我将控制器重建为 @ 987654326@ 并从内部调用Asset 类。

我的问题是,我在search_form_for 字段中查看asset_nameAsset 模型字段,但是当我按照我的布局编写视图时,我经常遇到路径错误。

有没有办法将不同的路径传递到 Ransack search_field 方法中,这样我就可以克服似乎与约定有关的问题?

如果您需要更多信息或我的问题不够清楚,请随时编辑或询问更多信息。谢谢大家!

【问题讨论】:

    标签: ruby-on-rails-4 ransack


    【解决方案1】:

    好的,我想通了。与我在其他形式中打破 Rails 命名约定时看到的问题类似,我必须在 search_form_for 辅助方法中传递 url 和方法。它正在工作!

    这是我现在对搜索的看法:

    <%= search_form_for(@q, url: "/inventory", method: :get) do |f| %>
      <%= f.search_field :asset_name_cont, placeholder: 'Search...' %>
    <% end %>
    

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 1970-01-01
      • 2012-07-19
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多