开发者

How do I create a button that enters a string into a form using Ruby on Rails?

开发者 https://www.devze.com 2022-12-19 04:54 出处:网络
Hello I am a Rails Noob so I apologize if this is elementary. I\'m creating a Twitter-like开发者_JAVA百科 application and working on a \'reply\' button that will automatically place a variable (the us

Hello I am a Rails Noob so I apologize if this is elementary. I'm creating a Twitter-like开发者_JAVA百科 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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消