Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, this is similar to what I imagined your set up might be.</p> <p>So you want <code>sender_id</code> to be the the id of the current user.</p> <p>So you should have:</p> <pre><code>def create @wallpost = current_user.sent_wallposts.create(params[:wall_post]) end </code></pre> <p>It doesn't matter then that <code>receiver_id</code> is mass_assigned as there is no problem assigning the receiver.</p> <p>It seems you are also interested in restricting the receiver_id. You can do that as well:</p> <pre><code>def create receiver_id = params[:wall_post].delete(:receiver_id) @wallpost = current_user.sent_wallposts.build(params[:wall_post]) @wallpost.receiver_id = receiver_id @wallpost.save end </code></pre> <p>However, that doesn't really change the situation at all since there is no different between that and mass assigning the value. The only reason you would want to avoid mass assignment of that value is if there is some additional logic that you want to put in place around whether a user can send to another user. In which case it would be something like this:</p> <pre><code>def create receiver_id = params[:wall_post].delete(:receiver_id) if current_user.can_send_to(receiver_id) @wallpost = current_user.sent_wallposts.build(params[:wall_post]) @wallpost.receiver_id = receiver_id @wallpost.save end end </code></pre> <p>You of course would need to create some method that can determine if the user can send to that receiver. And at this point your action is getting very complex and I would refactor this whole thing to take this out of your controller action.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload