简体   繁体   中英

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:

在此处输入图片说明

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

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>

需要person

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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