0
votes

My controller code is like these:

def edit
  @user = User.find params[:id]
end

def update
  @user = User.find params[:id]
  if @user.update_attributes(params[:user])
    flash[:notice] = "success!"
  else      
    flash[:notice] = "fail!"
  end
end

attr_accessor :username, :introduction, :first_name, :last_name

private

def user_params
  params.require(:user).permit(:username, :introduction, :first_name, :last_name)
end

View:

<%= form_for @user, :url =>edit_path(@user), :method => :PUT do |f| %>
<%= f.label :username %><br>
<%= f.text_field :username %>
<br>
<%= f.label :Introduction %><br>
<%= f.text_area :introduction %>
<br>
<%= f.label :FirstName %><br>
<%= f.text_field :first_name %>
<br>
<%= f.label :LastName %><br>
<%= f.text_field :last_name %>
<br>
<%= f.submit "Save changes"%><% end %>

And the terminal is:


    Started PUT "/profile/1/edit" for 127.0.0.1 at 2017-12-06 04:30:05 -0500
    Processing by ProfileController#edit as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"eBvvAG892DTOFzYuFTKPCgbOuSQXB8BVOub7A8CgaPHLW2vaBFMpUGNHhgYZednfd5pqICCy1DykIarD7QYtIg==", "user"=>{"username"=>"aaa", "introduction"=>"aaaaa", "first_name"=>"a", "last_name"=>"a"}, "commit"=>"Save changes", "id"=>"1"}
      User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
      Notification Load (0.1ms)  SELECT "notifications".* FROM "notifications"
      Chat Exists (0.0ms)  SELECT  1 AS one FROM "chats" WHERE "chats"."sender_id" = ? LIMIT 1  [["sender_id", 1]]
      Chat Exists (0.0ms)  SELECT  1 AS one FROM "chats" WHERE "chats"."recip_id" = ? LIMIT 1  [["recip_id", 1]]
      User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
      CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "1"]]
      Rendered profile/edit.html.erb within layouts/game (1.5ms)
      User Load (0.2ms)  SELECT "users".* FROM "users"
    Completed 200 OK in 32ms (Views: 28.5ms | ActiveRecord: 0.5ms)

But database is not updating:

1|[email protected]|$2a$11$2WHUjqQHU1gtlzw3ObXgVeE7dGG1qby2tLmATeoUyMnh1cg5wXv86||||3|2017-12-06 08:43:44.469588|2017-12-06 05:32:19.252680|127.0.0.1|127.0.0.1|2017-12-05 17:04:39.689636|2017-12-06 08:43:44.470487||1970-1-1|||This guy is lazy. Without an Introduction.|

2
Post code and logs, not images of code and logs.Sergio Tulentsev
I'm betting on validations.Sergio Tulentsev

2 Answers

1
votes

Try to use safe user_params instead of params[:user]:

if @user.update_attributes(user_params)
  # ...

BTW you probably don't need attr_accessor :username, :introduction, :first_name, :last_name line in your controller.

0
votes

Something is definitely askew. Look here:

Started PUT "/profile/1/edit" for 127.0.0.1 at 2017-12-06 04:30:05 -0500

You're doing a PUT to /profile/1/edit. Conventionally, you would do a GET to /profile/1/edit which would map to the edit action of your ProfileController. So, perhaps you have your routes mis-configured because I would normally expect that to throw an error. Instead (if your console output is correct), PUT "/profile/1/edit" is mapping to the edit action of your ProfileController, as you can see here:

Processing by ProfileController#edit as HTML

And, naturally, edit doesn't do any updating:

def edit
  @user = User.find params[:id]
end

So, no changes to your database.

By the way, are you using Devise or something like that? As you can see, you're loading your user twice:

  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  ...
  User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "1"]]

So I wonder if, in edit,

def edit
  @user = User.find params[:id]
end

@user = User.find params[:id] is even needed.