Hello - Noobie question I''m sure... I''m trying to pass a session value to a database field ''user'' when the user creates a new blog entry. The session value is created when the user logs on to the site. I have this in the view to create a new blog entry: <%= f.text_field :user, :value => session[:name], :disabled => true %> but it fails validation when I create the entry (validates_presence_of :user). Is there a way for me to store the session value into my database field ''user'' when the user creates the new entry (and prevent the user from manipulating the value)? Many thanks, Mark
On Sep 17, 1:51 am, Mark <polluxop...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello - > > Noobie question I''m sure... > > I''m trying to pass a session value to a database field ''user'' when the > user creates a new blog entry. The session value is created when the > user logs on to the site. I have this in the view to create a new > blog entry: > > <%= f.text_field :user, :value => session[:name], :disabled => true > %> >> but it fails validation when I create the entry > (validates_presence_of :user). Is there a way for me to store theDisabled form elements are not submitted by the browser. Even if this did work it would only stop the most casual of users form editing the submitted data. Why not do (in your controller''s create method) something.name session[:name] ? Fred> session value into my database field ''user'' when the user creates the > new entry (and prevent the user from manipulating the value)? > > Many thanks, > Mark
On Wed, Sep 16, 2009 at 7:51 PM, Mark <polluxopera-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is there a way for me to store the > session value into my database field ''user'' when the user creates the > new entry (and prevent the user from manipulating the value)?You can add stuff to the incoming "params" hash using merge. @user = User.find( session[:user_id] ) params[:blog].merge( :user => @user ) if @user Blog.create!( params[:blog] ) -- Greg Donald http://destiney.com/
That did it. Thanks! On Sep 16, 8:08 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sep 17, 1:51 am, Mark <polluxop...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hello - > > > Noobie question I''m sure... > > > I''m trying to pass a session value to a database field ''user'' when the > > user creates a new blog entry. The session value is created when the > > user logs on to the site. I have this in the view to create a new > > blog entry: > > > <%= f.text_field :user, :value => session[:name], :disabled => true > > %> > > > but it fails validation when I create the entry > > (validates_presence_of :user). Is there a way for me to store the > > Disabled form elements are not submitted by the browser. Even if this > did work it would only stop the most casual of users form editing the > submitted data. > Why not do (in your controller''s create method) something.name > session[:name] ? > > Fred > > > session value into my database field ''user'' when the user creates the > > new entry (and prevent the user from manipulating the value)? > > > Many thanks, > > Mark