ActionController::ParameterMissing in UsersController#create

流过昼夜 提交于 2019-12-11 23:18:06

问题


My error page tells me "param is missing or the value is empty: user" and references my user_params private method and my create action as the culprits. It also shows that all the params I'm passing in are getting picked up by the create post.

abbreviated Controller:

  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

My form_tag that initiates the create post:

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

回答1:


Change:

= form_tag @user do

to

= form_for @user do |f|

And then all label_tag to f.label, text_field_tag to f.text_field, email_field_tag to f.email_field and finally submit_tag to f.submit. Remove all nils and all should work.



来源:https://stackoverflow.com/questions/26737978/actioncontrollerparametermissing-in-userscontrollercreate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!