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 in the ‘create’ method
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.
leonel
August 11, 2011, 9:44pm
2
On 11 August 2011 20:21, Leonel . [email protected] wrote:
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]
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
leonel
August 11, 2011, 9:53pm
3
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.
leonel
August 11, 2011, 10:10pm
4
On 11 August 2011 20:53, Leonel . [email protected] wrote:
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
leonel
August 11, 2011, 10:44pm
5
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 = ???
I just tried @company.users [0].email and it also doesn’t work.
Started POST “/companies/45/users” for 127.0.0.1 at Thu Aug 11 15:37:28
-0500 2011
Processing by UsersController#create as HTML
Parameters: {“commit”=>“Add this person”,
“authenticity_token”=>“2sp49Lxt9a533aBCwdmiCAOmDSyfkO83dHpGI5JRFEE=”,
“utf8”=>“✓”, “company_id”=>“45”, “user”=>{“title”=>"",
“last_name”=>“Mini-Me”, “first_name”=>“Leo”,
“email”=>“[email protected] ”}}
leonel
August 12, 2011, 9:29am
6
On 11 August 2011 21:44, Leonel . [email protected] wrote:
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
leonel
August 12, 2011, 4:50am
7
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
===
Test2App::Application.routes.draw do
resources :users
root :to => “pages#home”
===
class UsersController < ApplicationController
def new
@user = User.new
@title = “Sign up”
end
def create
@user = User.new(params[:user])
@user.last_sent_to = @user.email
if @user.save
@title = "Home"
render 'pages/home'
else
@title = "Sign up"
render 'new'
end
end
def show
end
end
===
Pages#home
Find me in app/views/pages/home.html.erb
<%= link_to “New user form”, new_user_path %>
===
Users#new
Find me in app/views/users/new.html.erb
<%= form_for(@user ) do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.submit "Submit" %>
<% end %>
===
<%= @title %>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
<%= yield %>
===
http://localhost:3000 => home page
click link on home page => new.html.erb
fill in form and click submit => data for both :email and :last_sent_to
appears in database for data
leonel
August 12, 2011, 6:15pm
8
On 12 August 2011 16:55, Leonel . [email protected] wrote:
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
leonel
August 12, 2011, 5:55pm
9
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
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 }
else
format.html { render :action => “new” }
format.xml { render :xml => @company.errors , :status =>
:unprocessable_entity }
end
end
end
I did find I was getting a “mass-assign” error in the server log, so I
added the attribute to attr_accesssible in user.rb
attr_accessible :email,
:first_name,
:last_name,
:invitation_last_sent_at
[… more attributes …]
the mass-assign error went away but invitation_last_sent_to it’s still
nil.
I already tried in the console.
irb(main):001:0> u = User.new
=> #<User id: nil, email: nil, password_hash: nil, password_salt: nil,
created_at: nil, updated_at: nil, company_id: nil, first_name: nil,
last_name: nil, title: nil, username: nil, user_state: nil,
confirmation_token: nil, confirmed_at: nil, auth_token: nil, role_id:
nil, invitation_last_sent_at: nil, invitation_last_sent_to: nil>
irb(main):002:0> u.email = “[email protected] ”
=> “[email protected] ”
irb(main):003:0> u.invitation_last_sent_to = u.email
=> “[email protected] ”
So it’s working in the CONSOLE but not in the APP. I’m sure there is
something else in my code that it’s making it nil, will keep on looking.