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.

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

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

Really simple if you're using the RESTful_Authentication plugin (you really should if you're not)

f.hidden_field :author, current_user.login