armstheykill
2006-May-11 01:40 UTC
[Rails] RoR completely ignoring a column when saving changes
I have an action that needs to update two models/two tables in one shot,
and it works great, but ignores one column. Here is the action code:
(verify_id is irrelevant in this case)
[CODE]
def edit
@page_title = "Edit User"
@page_active_3 = "active"
if request.post?
@user = User.find(params[:id])
@profile = @user.profile
verify_id(@user.id, true)
begin
if @user.update_attributes(params[:user]) and
@profile.update_attributes(params[:profile])
flash[:notice] = ''User was successfully updated.''
redirect_to :action => ''view'', :id => @user.id
else
render :action => ''edit''
end
rescue
flash[:notice] = ''Errors occurred when attempting to update,
contact admin or check production.log''
redirect_to :action => ''view'', :id => @user.id
end
else # if GET request
@user = User.find(params[:id])
@profile = @user.profile
verify_id(@user.id, true)
end
end
[/CODE]
here is the view code:
[CODE]
<%= error_messages_for ''user'' %>
<%= error_messages_for ''profile'' %>
<div class="div_form">
<% if session[:admin] == ''yes'' -%>
<h2>Login Details</h2>
<p><label for="user_admin">Administrator
Access</label>
<%= select ''user'', ''admin'', [
[''no'', ''no''],
[''yes'', ''yes''] ] %></p>
<% end %>
<h2>Personal Profile</h2>
<p><label for="profile_first_name">First
Name</label><br />
<%= text_field ''profile'', ''first_name'',
:class => ''text_medium'' %></p>
<p><label for="profile_last_name">Last
Name</label><br />
<%= text_field ''profile'', ''last_name'',
:class => ''text_medium'' %></p>
<p><label for="profile_location">UY
Location</label>
<%= select ''profile'', ''location'', [
[''USA'', ''USA''],
[''South Africa'', ''South Africa''],
[''UK'', ''UK''],
[''South America'', ''South
America''],
[''Europe'', ''Europe''],
[''Asia'', ''Asia''] ] %>
</p>
<p><label for="profile_email">Email
Address</label><br />
<%= text_field ''profile'', ''email'',
:class => ''text_medium'' %></p>
</div>
[/CODE]
I suspect it may be a model validation problem, but i''ve removed all
validations for the edit/update action, but here is the model
[CODE]
require "digest/sha1"
class User < ActiveRecord::Base
# Validations
validates_uniqueness_of :login_name,
:message => ''has already been
taken'',
:on => :save, :on => :create
validates_presence_of :login_name, :password, :admin,
:message => ''is required'',
:on => :save, :on => :create
validates_associated :profile,
:on => :save, :one => :create
# Associations
belongs_to :profile, :foreign_key =>
''profile_id'',
:dependent => true
has_many :articles
# Attributes
attr_accessor :password
attr_accessible :login_name, :password
.... etc ....
[/CODE]
--
Posted via http://www.ruby-forum.com/.
www-data
2006-May-11 01:43 UTC
[Rails] Re: RoR completely ignoring a column when saving changes
I should obviously mention that RoR is ignoring the ''admin'' column of the ''user'' table which should be either ''yes'' or ''no'' . the column is formatted as a varchar(15) with a default of ''no'' -- Posted via http://www.ruby-forum.com/.