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

后端 未结 3 1263
予麋鹿
予麋鹿 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 14:44

    I think you should be able to do:

    <%= f.select :user_id, User.find(f.object.user_id).collect {|t| [t.user_name, t.id]} %>
    

    This does seem a little odd to me though. I'd have thought either:

    • Your object has a proper association to the relevant user, in which case you should be able to do f.object.user.user_name and f.object.user.id.
    • If you genuinely want the currently logged in user, you should probably be asking your authentication framework/code for the reference. E.g. if you were using Devise, it would be current_user.

    As an aside, I don't really understand why you want a select list just containing the current user - is that definitely what you're trying to achieve, or have I misunderstood?

提交回复
热议问题