Question

I'm trying to use ransack from a separate things controller using ryan bates' rails cast from here: http://railscasts.com/episodes/370-ransack

but when clicking on the Search Button in things/index.html.erb, I get:

AbstractController::ActionNotFound at /things
The action 'create' could not be found for ThingsController

things_controller.rb:

class ThingsController < ApplicationController
  before_filter :authorize
  respond_to :html

  def index

    # grab search parameters that user is asking for:
    @search = Bear.search(params[:q])

    @bears = @search.result
    @search.build_condition if @search.conditions.empty?
    @search.build_sort if @search.sorts.empty?

    respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @bears }
    end
  end

  def search
    index
    render :index
  end


end

routes.rb:

  resources :things do
    collection do
      match 'search' => 'things#search', :via => [:get, :post], :as => :search
    end
  end

things_helper.rb:

module ThingsHelper

  def link_to_add_fields(name, f, type)
    new_object = f.object.send "build_#{type}"
    id = "new_#{type}"
    fields = f.send("#{type}_fields", new_object, child_index: id) do |builder|
      render(type.to_s + "_fields", f: builder)
    end
    link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
  end

end

things/index.html.erb:

<h1>Things</h1>

<%= search_form_for @search, url: things_path, method: :post do |f| %>
  <%= f.condition_fields do |c| %>
    <%= render "condition_fields", f: c %>
  <% end %>
  <p><%= link_to_add_fields "Add Conditions", f, :condition %></p>
  <div class="field">
    Sort:
    <%= f.sort_fields do |s| %>
      <%= s.sort_select %>
    <% end %>
  </div>
  <div class="actions">
    <%= f.submit "Search" %>

    <a href="#" id="q_reset">Clear</a>

  </div>

<% end %>

<%= render 'results' %>

<script type="text/javascript">
  $("#q_reset").on('click', function(){
    $(".search-field").val('')
  });
</script>

_results.html.erb:

<p>SQL: <%= @bears.to_sql %></p>

<table id="report-table" class="table table-bordered tablesorter width-auto">
  <thead>
    <th class="padding-right-20">id</th>
    <th>name</th>
    <th class="padding-right-20">status</th>
  </thead>
  <tbody>
    <% @bears.each do |bear| %>
      <tr>
        <td><%= bear.id %></td>
        <td><%= bear.name %></td>
        <td><%= bear.status ? "active" : "inactive" %></td>
      </tr>
    <% end %>
  </tbody>
</table>

_condition_fields.html.erb:

<div class="field">
  <%= f.attribute_fields do |a| %>
    <%= a.attribute_select associations: [:animal] %>
  <% end %>
  <%= f.predicate_select %>
  <%= f.value_fields do |v| %>
    <%= v.text_field :value %>
  <% end %>
  <%= link_to "remove", '#', class: "remove_fields" %>
</div>

search.js.coffee:

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
jQuery ->
  $('form').on 'click', '.remove_fields', (event) ->
    $(this).closest('.field').remove()
    event.preventDefault()

  $('form').on 'click', '.add_fields', (event) ->
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
    event.preventDefault()
Was it helpful?

Solution

Try changing this line

<%= search_form_for @search, url: things_path, method: :post do |f| %>

to this line

<%= search_form_for @search, url: "/search", method: :post do |f| %>

Because things_path will take you to "/things" as post request.

If you run rake routes you will see, that "/things" with post request takes you things_controller, create action(things#create).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top