views:

49

answers:

2

Hello I am a Rails Noob so I apologize if this is elementary. I'm creating a Twitter-like application and working on a 'reply' button that will automatically place a variable (the username of the tweet's author) into the tweet form at the top of the page. This is what I have now:

def reply
    @tweet = Tweet.find(params[:id])
    @message = User.find_by_user_id(params[@tweet])
  end

I know that I'll have to change my routes but that's what I'm hung up on.

Any help would be greatly appreciated, thanks. I'm, again, a noob.

A: 

Your first line of code finds Tweet object. Then you put that tweet object into params hash as a key (this is the error). And AFAIK - you'd want to look into javascript that sets value for hidden field.

Eimantas
Thanks for your reply, how would I set this up in Java?
Java != Javascript. Try googling for "form manipulation with javascript".
Eimantas
A: 

This should work for you:

def reply
  @tweet = Tweet.find(params[:id])
  @message = @tweet.user.username
end

It assumes that the Tweet model has an association called user and that your User model has an attribute username:

class Tweet < ActiveRecord::Base
  belongs_to :user
  ...
end

class User < ActiveRecord::Base
  has_many :tweets
  ...
end

And this would probably match the current behaviour of twitter a bit better:

def reply
  @tweet = Tweet.find(params[:id])
  @message = "@" + @tweet.user.username + " "
end
Tomas Markauskas
Hey tomas, thanks for your reply. I get an 'Unknown action' response when I use this code. "No action responded to 160. Actions: create, current_user, current_user_flit?, destroy, logged_in?, login_required, redirect_to_target_or_default, and show" Any thoughts on how to correct? thanks.
What URL caused this? And how does your routes.rb file look like?
Tomas Markauskas