Set value in controller from a form

I have a typical rails form with form_for(@user...blah blah)

There is a field to enter the email address: email. But that same email address has to be entered into the invitation_last_sent_to. So the same value, in two different columns.

There is no problem when saving in the column 'email' but I want to save that same email value into invitation_last_sent_to column in the controller.

I have tried the following in users_controller.rb and when I get the rails console, the value in invitation_last_sent_to is always nil.

@user.invitation_last_sent_to = @user.email @user.invitation_last_sent_to = params[:user][:email] @user.invitation_last_sent_to = params[:email]

Please help.

Use ruby-debug to break into your code here and inspect the data and see what is going on. See the Rails Guide on debugging for how to do this.

I presume, though, that you are doing the above in the create action before the save, or in the update action before update_attributes.

Colin

I'm doing it in the create action. I did use debug to inspect @user and params and that's why I used...

@user.invitation_last_sent_to = @user.email @user.invitation_last_sent_to = params[:user][:email] @user.invitation_last_sent_to = params[:email]

...but none of those 3 seem to work.

I'm doing it in the create action. I did use debug to inspect @user and params and that's why I used...

@user.invitation_last_sent_to = @user.email @user.invitation_last_sent_to = params[:user][:email] @user.invitation_last_sent_to = params[:email]

...but none of those 3 seem to work.

so in the debugger what was the value of @user.email after @user = User.new(params[:user]) and what was the value of @user.invitation_last_sent_to after @user.invitation_last_sent_to = @user.email

If you don't understand what you see in the debugger copy the output from the terminal showing the above tests and paste here along with the code for the create action.

Colin

Is it maybe because it's a nested resource?

I have this in users_controller.rb

Leonel *.* wrote in post #1016258:

Is it maybe because it's a nested resource?

Possibly. This certainly works:

class PagesController < ApplicationController   def home     @title = "Home"   end

end

Is it maybe because it's a nested resource?

I have this in users_controller.rb ----------------------------------------- @user = @company.users.build(params[:user]) @user.invitation_last_sent_at = Time.now @user.invitation_last_sent_to = ???

What? You have that line if the controller?

------------------------------------------------------

I just tried @company.users[0].email and it also doesn't work.

What do you mean it doesn't work?

You need to give more information not vague comments. Show us the results of what you are doing in the console by copying and pasting the console output here.

Colin

I have this in users_controller.rb ----------------------------------------- @user = @company.users.build(params[:user]) @user.invitation_last_sent_at = Time.now @user.invitation_last_sent_to = ???

What? You have that line if the controller?

No, I meant I tried those 3 different lines in different times. Here's the create method in the users_controller.rb

I have this in users_controller.rb ----------------------------------------- @user = @company.users.build(params[:user]) @user.invitation_last_sent_at = Time.now @user.invitation_last_sent_to = ???

What? You have that line if the controller?

No, I meant I tried those 3 different lines in different times. Here's the create method in the users_controller.rb ------------------------------------------- def create @company = Company.find(params[:company_id]) @user = @company.users.build(params[:user]) @user.user_state = "invited" @user.invitation_last_sent_at = Time.now @user.invitation_last_sent_to = @user.email

Break in here with the debugger and see what is happening.

respond_to do |format| if @user.save Notifier.user_invited(@user).deliver format.html { redirect_to(users_url, :notice => user_long_name) } format.xml { render :xml => @user, :status => :created, :location => @company }

Presumably you are seeing the above happening, so showing that the save is working.

else format.html { render :action => "new" } format.xml { render :xml => @company.errors, :status => :unprocessable_entity }

and presumably the above is not happening.

Colin