Ransack - 无法找到“创建”操作

时间:2013-12-04 19:42:51

标签: ruby-on-rails ransack

我正在尝试使用来自这里的ryan bates'rails来从单独的东西控制器中使用ransack:http://railscasts.com/episodes/370-ransack

但是当点击things / index.html.erb中的搜索按钮时,我得到:

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

东西/ 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()

1 个答案:

答案 0 :(得分:2)

尝试更改此行

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

到这一行

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

因为things_path会将你带到“/ things”作为帖子请求。

如果你运行rake routes,你会看到,带有帖子请求的“/ things”会带你到thing_controller,创建动作(#draws)。