Rails形成了创建动作而不是索引动作

时间:2018-05-23 19:56:02

标签: ruby-on-rails

我正在尝试创建一个搜索表单,该表单将获取结果并将param推送到Search控制器的索引中。但是,即使我在表单中指定了get方法,我仍然会收到错误The action 'create' could not be found for SearchController,即使我正在尝试加载索引。

家/ index.html.erb

<%= form_tag(controller: "search", :url => search_index_path, :method 
=> :get) do %>
<%= label_tag(:name, "Search for:") %>
<%= text_field_tag(:name) %>
<%= submit_tag("Search") %>
<% end %>

search_controller.rb

class SearchController < ApplicationController
  def index
    @search = Tmdb::Search.tv(params[:name])
  end
  def show
  end
end

的routes.rb

Rails.application.routes.draw do
 root 'home#index'
 resources :home
 resources :search
end

表格输出:

<form action="/search?method=get" accept-charset="UTF-8" method="post" abineguid="2F75F785E35A4ED5B614E26762C7B53B"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="6dKa4a+vBvybu9b4eYwuEqx9dNcvadCyqRz7ox+ggW2YVAWq6Y8BWVNHx8nDWJbnQ4hTl5OTi3TSkjUfFuHbCQ==">
<label for="name">Search for:</label>
<input type="text" name="name" id="name">
<input type="submit" name="commit" value="Search" data-disable-with="Search">
</form>

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您对form_tag的调用是错误的。

form_tag(url_for_options = {},options = {},&amp; block)

您将所有选项传递给url_for_options,包括:method。 你应该这样做:

<%= form_tag({ controller: "search", action: "index" }, { :method 
=> :get }) do %>

或更简单:

<%= form_tag(search_index_path, :method => :get) do %>
<%= label_tag(:name, "Search for:") %>
<%= text_field_tag(:name) %>
<%= submit_tag("Search") %>
<% end %>

请参阅:https://apidock.com/rails/ActionView/Helpers/FormTagHelper/form_tag