Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21996

ActionController::ParameterMissing

I have made an application that has a personsController class, with an action show and new. I also created the Person model this way:

rails generate model Person name:string surname:string partner:references

The partner field should reference another person. In the new action I created a form to insert a new person into the database:

<%= form_for :person , url: persons_path do |f| %>
    <p>
        <%= f.label :name %>
        <%= f.text_field :name %>
    </p>
    <p>
        <%= f.label :surname %>
        <%= f.text_field :surname %>
    </p>
    <p>
        <%= f.label :partner %>
        <%= f.number_field :partner , value: 0 %>
    </p>
    <p>
        <%= f.submit %>
    </p>
<% end %>

This is the personsController method create:

def create
    @person= Person.new(post_params)
    @Person.save
    redirect_to @person

private
    def post_params
        params.require(:post).permit(:partner,:name,:surname);
    end

The problem is that I get an error when I submit the form:

enter image description here

Update

I changed the instruction to:

params.require(:person).permit(:partner,:name,:surname);

But I still get an error:

NameError in PersonsController#create
uninitialized constant Person::Partner

Person model:

class Person < ActiveRecord::Base
  belongs_to :partner
end

Upvotes: 1

Views: 1358

Answers (2)

bjhaid
bjhaid

Reputation: 9782

This is the problem here:

From your model post is not a defined attribute

so changed this:

    def post_params
        params.require(:post).permit(:partner,:name,:surname);
    end

to

    def post_params
        params.require(:person).permit(:partner_id,:name,:surname)  
    end

EDIT

use partner_id instead of partner for your foreign key

<p>
    <%= f.label :partner %>
    <%= f.number_field :partner_id, value: 0 %>
</p>

Upvotes: 3

NARKOZ
NARKOZ

Reputation: 27951

Require person:

params.require(:person).permit(:partner, :name, :surname)

Upvotes: 2

Related Questions