简体   繁体   English

Ruby on Rails 4-表单助手未将参数哈希传递给控制器

[英]Ruby on Rails 4 - Form helper not passing a params hash to controller

I'm having some issues with my view not passing a params hash to the controller. 我的视图无法传递参数哈希值到控制器时遇到了一些问题。 I have a form defined in a view that will add a new item to a database and I am getting the error: 我在视图中定义了一个表单,该表单会将新项目添加到数据库中,并且出现错误:

ActionController::ParameterMissing:
  param not found: item

Here are my code files for the form helper in question and my controller that handles the action. 这是我的表单帮助程序的代码文件以及处理该操作的控制器。

new.hmtl.erb 新的hmtl.erb

<%= form_for(@item) do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>

  <%= f.label :description %>
  <%= f.text_field :description %>

  <%= f.submit "Add", class:"btn btn-large btn-primary %>
<% end %>

items_controller.rb items_controller.rb

class ItemsController < ApplicationController
  def new
    @item = Item.create(set_params)
  end

  private
    def set_params
      params.require(:item).permit(:name, :description)
    end
  end
end

I've yet to see a solid answer on how to fix this from the other questions I've seen, I think. 我认为,我还没有看到关于如何解决其他问题的可靠答案。

Your new action is the one that renders the form to create a new Item. 您的new操作是呈现表单以创建新项目的操作。 When this action is rendered, your params are blank. 呈现此动作后,您的参数为空白。

Your create action is the one that processes the form, where your params will be populated. 您的create动作是处理表单的动作,将在其中填充参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM