Forum: Ruby on Rails updating a record error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Maulin p. (Guest)
on 2008-12-17 03:53
I have having some trouble with my update statement. I have a page that
allows users to change their personal information (name,
password...etc), The update statement seems to execute successfully but
does not update anything. What am I missing?

MODEL CODE
-----------
class Player < ActiveRecord::Base
  has_many :statistics
  has_many :ladders, :through => :statistics

  validates_presence_of :first_name, :last_name, :login, :password
end


CONTROLLER CODE
----------------
  def edit
    @player = Player.find(params[:id])
  end

  def update
    Player.update(params[:id],
    {:first_name => params[:first_name],
    :last_name => params[:last_name],
    :login => params[:login],
    :password => params[:password]})
    flash[:notice] = "Personal information updated sucessfully
#{params[:id]}"
    redirect_to player_path(@current_player)
  end


VIEW CODE
----------
The edit method is called from another view as follows

<p>
  <% form_tag(edit_player_path(@player), :method => "get") do %>
    <%= submit_tag 'Edit personal information' %>
  <% end %>
</p>

EDIT VIEW
----------
<h2> Edit personal information </h2>
<%= render :partial => 'form' %>

FORM VIEW
----------
<%= error_messages_for 'player' %>
<% form_for(@player) do |f| %>
  <p>
  First Name:<br>
  <%= f.text_field :first_name %>
  </p>
  <p>
  Last Name:<br>
  <%= f.text_field :last_name %>
  </p>
  <p>
  Username:<br>
  <%= f.text_field :login %>
  </p>
  <p>
  Password:<br>
  <%= f.password_field :password %>
  </p>
  <p>
  <%= submit_tag "Save" %>
  </p>
<% end %>
Hassan S. (Guest)
on 2008-12-17 04:56
(Received via mailing list)
On Tue, Dec 16, 2008 at 5:53 PM, Maulin P.
<removed_email_address@domain.invalid> wrote:
>
> I have having some trouble with my update statement. I have a page that
> allows users to change their personal information (name,
> password...etc), The update statement seems to execute successfully but
> does not update anything. What am I missing?

>  def update
>    Player.update(params[:id],
>    {:first_name => params[:first_name],

Per the api doc, ActiveRecord::Base.update
"Updates an object (or multiple objects) and saves it to the database,
if validations pass. The resulting object is returned whether the object
was saved successfully to the database or not."

Are you logging validation errors on this? If not, or just for grins,
you
might change the controller to Player.update_attributes! to try to make
the error visible...

Alternatively, does your DB log show any problems?

FWIW,
--
Hassan S. ------------------------ removed_email_address@domain.invalid
This topic is locked and can not be replied to.