Question

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

<%= 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

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.

Was it helpful?

Solution

Your new action is the one that renders the form to create a new Item. 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.

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