John Karver
John Karver

Reputation: 39

Rails (param is missing or the value is empty)

I am using rails 4 and I've read that attr_accessible is beter not to use in this version. I have created that code:

class UsersController < ApplicationController
  def new
    @user = User.new(user_params)
  end

  private
  ## Strong Parameters 
  def user_params
    params.require(:user).permit(:name, :email, :password, :password_confirmation)
  end
end

But it gives me out this error:

ActionController::ParameterMissing in UsersController#new
param is missing or the value is empty: user

I am trying to display that html.erb:

<%= form_for :user do |f| %>
    <%= f.text_field :name %>
    <%= f.text_field :email %>
    <%= f.text_field :password %>
    <%= f.text_field :password_confirmation %>
    <%= f.label :submit %>
<% end %>

Any solutions?

Upvotes: 0

Views: 1123

Answers (2)

Vrushali Pawar
Vrushali Pawar

Reputation: 3803

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to user_path(@user)
    else
      render :action => 'new'
    end
  end 

  private
  def user_params
    params.require(:user).permit(:name, :email, :password, :password_confirmation)
  end
end

Basically while instantiating an object, you don't need to call user_params .

Upvotes: 0

steve klein
steve klein

Reputation: 2629

Typically the new action would be @user = User.new as there are no user_params getting posted back from the view.

Upvotes: 2

Related Questions