Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Couldn't you just have the form on the post's show form? And then pass the form values to your messages create action?</p> <p>./app/controllers/message_controller.rb</p> <pre><code>class MessageController &lt; ApplicationController def create @message = Message.create(create_message_params) @message.send end private def create_message_params {}.tap do |h| h[:from_user_id] = params[:from_user_id] h[:to_user_id] = params[:to_user_id] h[:text] = params[:text] end end end </code></pre> <p>./app/controllers/post_controller.rb</p> <pre><code>class PostController &lt; ApplicationController def show @post = Post.find(params[:id]) @message = Message.new end end </code></pre> <p>./app/views/posts/show.html.erb</p> <pre><code>&lt;!-- omitting other post show html, showing just the message form --&gt; &lt;% form_for(@message, url: messages_path do |f| %&gt; &lt;%= hidden_field_tag(:to_user_id, @post.author.id ) %&gt; &lt;%= text_area_tag(:text, @message.text) %&gt; &lt;br/&gt; &lt;%= f.submit("Send Message") %&gt; &lt;% end %&gt; </code></pre> <p><strong>Edit 2013.08.25 from the comments, the wish was for the messages to be in a different view</strong></p> <p>First you'd have a link in the Post's "show" view:</p> <pre><code>&lt;%= link_to "Send a message", new_messages_path(to_user_id: @post.author.id) %&gt; </code></pre> <p>Then you'd have to create the new action, and a view, which takes in the incoming <code>to_user_id</code>, and stores it in a hidden field, perhaps. Then when they post to the <code>message_path</code> by submitting that messages form, you'd have the <code>to_user_id</code>, and the message, coupled with the current_user.id. </p> <p>Does that make sense?</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