Ask A Question

Notifications

You’re not receiving notifications from this thread.

Ransack undefined method path in Rails 4.2.1

shakycode asked in General

I'm working on a 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 methodassets_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!

Reply

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 %>
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.