Ok I just added a collection_select tag to the form and attempted to
save it but the value is not getting saved. This is what my mvc looks
like :
V (abbreviated edit.html):
<table>
<% form_for :user, :url => user_url(@user), :html => { :method =>
:put }
do |f| %>
<tr>
<td class="tlabel"><label
for="email">Email:</label></td>
<td><%= f.text_field :email %></td>
</tr>
<tr>
<td class="tlabel"><label
for="customer_id">Customers:</label></td>
<td><%= f.collection_select(:customer_id,@customer_accounts,:id,
:name, :include_blank=>true) %></td>
<tr>
<td colspan="2" align="center"><br /><%=
image_submit_tag(''Save.png'')
%></td>
</tr>
<% end %>
</table>
M (abbreviated user.rb):
class User < ActiveRecord::Base
validates_presence_of :email
belongs_to :customer
end
C (abbreviated users_controller.rb):
def edit
@customer_accounts = Customer.find(:all)
@user = current_user
end
def update
@user = User.find(current_user)
@customer_accounts = Customer.find(:all)
@profile = @user.user_profile
if @user.update_attributes(params[:user])
logger.debug(">>>> user.customerid :
#{@user.customer_id}")
if @profile.update_attributes(params[:user][:profile_attributes])
flash[:notice] = "User updated"
redirect_to :action => ''show'', :id =>
current_user
else
render :action => ''edit''
end
else
render :action => ''edit''
end
end
--
I tried to output customer_id in the controller and its a blank! When I
look at the html source of the page in firebug I see that the accounts
field seems to be properly generated:
<td>
<select id="user_customer_id"
name="user[customer_id]">
<option value=""/>
<option value="1">Army</option>
<option value="2">Navy</option>
<option value="3">Marines</option>
<option value="4">Air Force</option>
</select>
</td>
Any help would be great.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Is it possible that params[:user][''customer_id''] is coming through from the form as a string and you need to to_i it before the update_attributes since customer_id is an integer? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Cayce Balara wrote:> Is it possible that params[:user][''customer_id''] is coming through from > the form as a string and you need to to_i it before the > update_attributes since customer_id is an integer?I didnt try that but I figured out the error. It was with the controller code; I needed to set the user.customer to @customer and then save that user .It was just the addition of these two lines : @user.customer = @customer @user.save! Following is the over all code : def update @user = User.find(current_user) @profile = @user.user_profile logger.debug(">>>> customer : #{params[:user][:customer_id]}") @customer= Customer.find(params[:user][:customer_id]) logger.debug(">>>> customer : #{@customer.name}") if @user.update_attributes(params[:user]) logger.debug(">>>> user.customerid : #{@user.customer_id}") @user.customer = @customer logger.debug(">>>> user.customerid : #{@user.customer_id}") @user.save! if @profile.update_attributes(params[:user][:profile_attributes]) flash[:notice] = "User updated" redirect_to :action => ''show'', :id => current_user else render :action => ''edit'' end else render :action => ''edit'' end end Thanks for the help! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
That certainly is a fix - but note you are making what should be an unnecessary database call with the .find method on Customer. Since you already have the customer_id you should be able to just store it and move on, without having to go look up the customer again in the database. Although, doing it your way does provide some protection against rogue requests coming in from somewhere other than your form. But, you''ll also want to make sure you prepare for the possibility that the .find call doesn''t return anything and catch that exception. Your solution even more makes me think it was a string_to_integer issue, since ActiveRecord .find works fine with a string "1" or an integer. But, no worries - you have a fix - good work! Ather Shiraz wrote:> Cayce Balara wrote: >> Is it possible that params[:user][''customer_id''] is coming through from >> the form as a string and you need to to_i it before the >> update_attributes since customer_id is an integer? > > I didnt try that but I figured out the error. It was with the controller > code; I needed to set the user.customer to @customer and then save that > user .It was just the addition of these two lines : > > @user.customer = @customer > @user.save! > > Following is the over all code : > > def update > @user = User.find(current_user) > @profile = @user.user_profile > logger.debug(">>>> customer : #{params[:user][:customer_id]}") > @customer= Customer.find(params[:user][:customer_id]) > logger.debug(">>>> customer : #{@customer.name}") > if @user.update_attributes(params[:user]) > logger.debug(">>>> user.customerid : #{@user.customer_id}") > @user.customer = @customer > logger.debug(">>>> user.customerid : #{@user.customer_id}") > @user.save! > if @profile.update_attributes(params[:user][:profile_attributes]) > flash[:notice] = "User updated" > redirect_to :action => ''show'', :id => current_user > else > render :action => ''edit'' > end > else > render :action => ''edit'' > end > end > > > Thanks for the help!-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---