0

メールボックス ジェムを使用して、Rails アプリのユーザー マッサージに取り組んでいます。

何らかの理由で、メッセージ フォームをレンダリングできず、user.id などの値が渡されません。これを行う方法は何ですか? 私は私が近いことを知っていますが、私はこれでハードコアを愛撫しています 私のビュー/投稿/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 % -->

新しいメッセージ パスをクリックすると、次のページに移動します: 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 %>

投稿のユーザー名の値を渡すか、post.email をメッセージ フォームに渡したいと思います。

私がそれをやろうとした別の方法で、私が好む方法は <%= render 'messages/form', conversation: conversation %> ですが、そうすると

undefined method error for 'conversation'

ビュー/メッセージ/_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 %>

基本的には投稿に返信ボタンをつけて、投稿したユーザーにメッセージを送れるようにしたいです。私はdeviseを使用しており、すべてのユーザーがログインしています。

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 に答える