我的错误页面告诉我" param丢失或值为空:user"并引用我的user_params私有方法和我的创建操作作为罪魁祸首。它还表明,我传入的所有参数都被创建帖子接收。
缩写控制器:
def new
@user = User.new
end
def create
@user = User.new(user_params)
@user.save
redirect_to @user
end
private
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:first_name, :last_name, :email)
end
我的form_tag启动了创建帖子:
= form_tag @user do
.form-group
= label_tag :first_name, nil, class: 'col-md-2'
= text_field_tag :first_name, nil, class: 'col-md-3'
%br
.form-group
= label_tag :last_name, nil, class: 'col-md-2'
= text_field_tag :last_name, nil, class: 'col-md-3'
%br
.form-group
= label_tag :email, nil, class: 'col-md-2'
= email_field_tag :email, nil, class: 'col-md-3'
%br
.actions.form-group
= submit_tag 'Submit', class: 'btn btn-primary col-md-offset-2'
答案 0 :(得分:1)
变化:
= form_tag @user do
到
= form_for @user do |f|
然后所有label_tag
到f.label
,text_field_tag
到f.text_field
,email_field_tag
到f.email_field
,最后submit_tag
到{ {1}}。删除所有的nils,一切都应该有效。