【问题标题】:Rails: Create action is fired even with "GET" method and "index" action specifiedRails:即使指定了“GET”方法和“index”操作,也会触发创建操作
【发布时间】:2014-08-10 20:47:46
【问题描述】:

我的索引视图有一个form_tag 用于过滤:

= form_tag action: "index", method: "get" do
  = label_tag(:sport_id, "Filter by sport:")
  = select_tag(:sport_id, options_from_collection_for_select(Sport.all, :id, :name))
  = submit_tag("Filter")

团队控制者:

def index
  @teams = Team.paginate(page: params[:page])
  # Note: this does not do any filtering yet.
end

当我提交表单时,我收到此错误:

TeamsController#create 中的 ActionController::ParameterMissing

参数缺失或值为空:团队

我不明白为什么 create 操作一开始就会触发。

【问题讨论】:

    标签: ruby-on-rails filtering


    【解决方案1】:

    form_tag 接收到的第一个哈希条目用于 url_for_options 哈希

    请参阅此文档http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag

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

    您需要明确指出,method 条目是选项哈希的一部分

    试试……

    form_tag {action: "index"}, method: :get do
    

    或者...

    form_tag '/teams', method: :get do
    

    【讨论】:

    猜你喜欢
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多