0

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.

4

1 回答 1

1

您应该在您的 routes.rb 中定义嵌套在 post 下的新消息...

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

这会给你一个路径 new_post_message GET /posts/:posts_id/messages/new messages#create

您可以将帖子指定为链接的一部分

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

这会将 param[:post_id] 传递给消息控制器中的 create 方法,因此您可以检索帖子,将其设置为实例变量,并以 view/messages/new 格式使用它。

于 2014-04-14T23:14:25.077 回答