I want to return a single result using .find() with Ruby on Rails

后端 未结 3 1283
予麋鹿
予麋鹿 2021-01-21 14:19

I have a user view and a rental view. In my rental view im trying to show the current users name. I think I am pretty close but I can\'t work out this last bit.

This ret

3条回答
  •  孤独总比滥情好
    2021-01-21 15:02

    I am assuming you have a rental object, for which you show the form, I assume it is an instance variable @rental, furthermore I assume that inside your Rental class there is the following relation

    class Rental
    
      belongs_to :user
    
    end
    

    Then you could just write the following:

    f.select :user_id, [[@rental.user.user_name, @rental.user.id]]
    

    Hope this helps.

    On a related but less important note: it is really weird to have a column called user_name for a user: I would call that column just name, since it is part of a user anyway.

提交回复
热议问题