Question

I have been working on user massaging for my rails app using the mailboxer gem.

For some reason I can't render the messages form and my values aren't being passed, like user.id What's a way of doing this? I know I'm close but I'm fondling hardcore with this one In my views/posts/show.html.erb:

<strong>title & author</strong>
<h2><%= @post.title %></h2>

   <p><%= @post.author %></p>
</div>

<strong>course</strong><br />
<h1><%= @post.course %></h1>



<strong>school</strong><br />
<h1><%= @post.school %></h1>


 <strong>price</strong><br />
<h1><%= @post.price %></h1>

<p>
  <strong>Posted by</strong><br />
  <%= @post.email %>
    </p>
    <br />
  <h2>Description</h2>
   <h1><%= word_wrap(@post.description, :line_width => 8) %></h1>


   <br />
    DATE POSTED: <%= @post.created_at %><br /><br />


   </div>

  <%= link_to "reply", new_message_path %>
<!--%= render 'messages/form', conversation: conversation % -->

When I click on new message path this is the page it goes to: view/messages/new.html.erb

Send a message to

<%= @user.username %>
<%= form_tag({controller: "messages", action: "create"}, method: :post) do %>
 <%= label_tag :subject %>
 <%= text_field_tag :subject %>
<%= label :body, "Message text" %>
<%= text_area_tag :body %>
<%= hidden_field_tag(:user, "#{@user.id}") %>
<%= submit_tag 'Send message' %>
<% end %>

I would like to pass the value of the username of the post, or pass post.email to the messages form.

The other way I tried to do it, and the way I prefer of doing it is to <%= render 'messages/form', conversation: conversation %> but when I do that I get

undefined method error for 'conversation'

views/messages/_form.html.erb:

Reply:
 <%= form_for :message, url: [:reply, conversation] do |f| %>
 <%= f.text_area :body %>
 <%= f.submit "Send Message", class: 'btn btn-primary' %>
 <%= submit_tag 'Clear Reply Box', type: :reset, class: 'btn btn-danger' %>
 <% end %>

Basically I want to add a reply button to a post and let the user send a message to the user who posted. I'm using devise and all users are logged in.

Was it helpful?

Solution

You should define the new message nested under post in your routes.rb...

  resources :posts do
    resources :messages, only: [:new, :create]
  end

That will give you a path new_post_message GET /posts/:posts_id/messages/new messages#create

You would specify the post as part of the link

<%= link_to 'reply', new_post_message_path(@post) %>

That passes the param[:post_id] to the create method in the messages controller so you can retrieve the post, set it to an instance variable, and use it in the view/messages/new format.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top