tefflox
2008-Jul-07 21:53 UTC
How to get a users screen name into a hidden field in a PostsController action?
Instead of the user entering a name, I want to pass their screen name
along with supplied :title and :content fields. here is what I''ve
got..
f.hidden_field :author, user.screen_name
here is the create method from Posts controller:
def create
@title = "Gravity"
@user = User.find(params[:id]) # this does not work, and I don''t
know why
if request.post? and params[:post]
if new
flash[:notice] = "Comment created"
redirect_to :action => "index"
else
flash[:notice] = "Please complete all fields"
redirect_to :action => "create"
end
end
end
Please help.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Guillaume Petit
2008-Jul-08 08:52 UTC
Re: How to get a users screen name into a hidden field in a PostsController action?
Jesse Crockett wrote:> Instead of the user entering a name, I want to pass their screen name > along with supplied :title and :content fields. here is what I''ve > got.. > > f.hidden_field :author, user.screen_name > > here is the create method from Posts controller: > > def create > @title = "Gravity" > @user = User.find(params[:id]) # this does not work, and I don''t > know why > if request.post? and params[:post] > if new > flash[:notice] = "Comment created" > redirect_to :action => "index" > else > flash[:notice] = "Please complete all fields" > redirect_to :action => "create" > end > end > end > > Please help.I would need more information but I''ll try to guess, and since you are using f.hidden_field you should have a form declaration related to a particular object (let''s say "comment"). In that case your author information should be accessible through params[:comment][:author] in your controller. That means you should change the line @user = User.find(params[:id]) in @user = User.find_by_screen_name(params[:comment][:author]) Don''t forget to change :comment by the good name. If not, then give us more information. Guillaume http://www.nomadsper.com -- 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 -~----------~----~----~----~------~----~------~--~---
Pardee, Roy
2008-Jul-08 12:23 UTC
Re: How to get a users screen name into a hidden field in a PostsController action?
What does the html generated from your hidden_field look like? Is
user.screen_name getting copied over properly?
If it *is*, you should expect to find it in params[:author] in your
create method I think.
Also, you don''t seem to be saving anything to the db in that method--I
would expect to see something like:
@post = Post.new(params[:post])
@post.save
In there somewhere.
HTH,
-Roy
-----Original Message-----
From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
[mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of
tefflox
Sent: Monday, July 07, 2008 2:53 PM
To: Ruby on Rails: Talk
Subject: [Rails] How to get a users screen name into a hidden field in a
PostsController action?
Instead of the user entering a name, I want to pass their screen name
along with supplied :title and :content fields. here is what I''ve
got..
f.hidden_field :author, user.screen_name
here is the create method from Posts controller:
def create
@title = "Gravity"
@user = User.find(params[:id]) # this does not work, and I don''t
know why
if request.post? and params[:post]
if new
flash[:notice] = "Comment created"
redirect_to :action => "index"
else
flash[:notice] = "Please complete all fields"
redirect_to :action => "create"
end
end
end
Please help.
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Matt Darby
2008-Jul-08 12:35 UTC
Re: How to get a users screen name into a hidden field in a PostsController action?
On Jul 7, 5:53 pm, tefflox <teff...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Instead of the user entering a name, I want to pass their screen name > along with supplied :title and :content fields. here is what I''ve > got.. > > f.hidden_field :author, user.screen_nameReally simple if you''re using the RESTful_Authentication plugin (you really should if you''re not) f.hidden_field :author, current_user.login --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---