# How to get a users screen name into a hidden field in a PostsController action?

**URL:** <https://www.ruby-forum.com/t/how-to-get-a-users-screen-name-into-a-hidden-field-in-a-postscontroller-action/142436>\
**Category:** Rails\
**Created:** [July 7, 2008, 11:54pm UTC](https://www.ruby-forum.com/t/how-to-get-a-users-screen-name-into-a-hidden-field-in-a-postscontroller-action/142436 "2008-07-07T23:54:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![caiwangqin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4bbf92/32.png) [@caiwangqin](https://www.ruby-forum.com/u/caiwangqin)\
**Post date:** [July 7, 2008, 11:54pm UTC](https://www.ruby-forum.com/t/how-to-get-a-users-screen-name-into-a-hidden-field-in-a-postscontroller-action/142436/1 "2008-07-07T23:54:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![caiwangqin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4bbf92/32.png) [@caiwangqin](https://www.ruby-forum.com/u/caiwangqin)\
**Post date:** [July 8, 2008, 10:52am UTC](https://www.ruby-forum.com/t/how-to-get-a-users-screen-name-into-a-hidden-field-in-a-postscontroller-action/142436/2 "2008-07-08T10:52:33Z")

</div>

Jesse C. 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](http://www.nomadsper.com)

---

<div class="post-metadata">

**Author:** ![caiwangqin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4bbf92/32.png) [@caiwangqin](https://www.ruby-forum.com/u/caiwangqin)\
**Post date:** [July 8, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/how-to-get-a-users-screen-name-into-a-hidden-field-in-a-postscontroller-action/142436/3 "2008-07-08T14:36:14Z")

</div>

On Jul 7, 5:53 pm, tefflox [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

Really simple if you’re using the RESTful\_Authentication plugin (you  
really should if you’re not)

f.hidden\_field :author, current\_user.login

---

<div class="post-metadata">

**Author:** ![caiwangqin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4bbf92/32.png) [@caiwangqin](https://www.ruby-forum.com/u/caiwangqin)\
**Post date:** [July 8, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/how-to-get-a-users-screen-name-into-a-hidden-field-in-a-postscontroller-action/142436/4 "2008-07-08T14:36:14Z")

</div>

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
