user5859747
user5859747

Reputation:

Rails added model attribute not filling from form

I added an attribute to a model. The form field populates, but when I submit, it doesn't input it into my DB.

The field is RoleID:

enter image description here

You can see on the next page that it does not fill the DB:

enter image description here

I have also confirmed from the DB:

+----+--------+-------------------------+----------+---------+-------+---------------------+---------------------+---------+
| id | userid | email                   | password | fname   | lname | created_at          | updated_at          | roleids |
+----+--------+-------------------------+----------+---------+-------+---------------------+---------------------+---------+
|  1 |      1 | [email protected]  | test     | kendall | weihe | 2016-03-15 21:56:30 | 2016-03-15 21:56:30 |    NULL |
|  2 |      2 | [email protected] | test     | ken     | weihe | 2016-03-16 11:39:05 | 2016-03-16 11:39:05 |    NULL |
|  3 |      2 | [email protected] | test     | ken     | weihe | 2016-03-16 11:42:32 | 2016-03-16 11:42:32 |    NULL |
|  4 |      2 | [email protected] | test     | ken     | weihe | 2016-03-16 11:44:26 | 2016-03-16 11:44:26 |    NULL |
+----+--------+-------------------------+----------+---------+-------+---------------------+---------------------+---------+

The input form looks like this:

  <div class="field">
    <%= f.label :lname %><br>
    <%= f.text_field :lname %>
  </div>
  <div class="field">
    <%= f.label :roleids, "RoleID: 1 for Employee or 2 for Customer" %><br>
    <%= f.number_field :roleids %>
    <%debugger%>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>

The debugger placed above confirms that the attribute exists:

#<User id: nil, userid: nil, email: nil, password: nil, fname: nil, lname: nil, created_at: nil, updated_at: nil, roleids: nil>

The controller action looks like this:

  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

Upvotes: 0

Views: 90

Answers (1)

Gagan Gami
Gagan Gami

Reputation: 10251

check you have added roleids attribute to your strong parameter in your controller like:

def user_params
  params.require(:user).permit(:email, :password, :password_confirmation, :fname, :lname, :roleids)
end

Upvotes: 1

Related Questions