So, what I wanted to do was visit the user profile, click "Send message" and write a letter, and it will automatically send it to the user without explicitly specifying a field :.
The problem was that user_id in <%= hidden_tag_field :user_id %> not set. In other words, I could not get: user_id from params while in this form.
Some of the solutions we tried to include included params in link_to, but this did not match the forms that saw the object as nil.
As a result, I created such a nested resource:
resources :users do resources :messages do end end
And that eventually gave me the url: users /: id / messages / new ( new_user_message_path )
My controller turned out like this:
def new @message = ActsAsMessageable::Message.new @user = params[:user_id] end def create @to = User.find params[:id] if current_user.send_message(@to, params[:acts_as_messageable_message][:body] flash[:notice] = "Success" else flash[:error] = "Fail" end end
In the form, I managed to leave <%= hidden_tag_field :user_id %> as it is.
But basically this solved the problem of finding the user (whose profile I was visiting) and setting @to in my creation action.
source share