2016-09-12 9 views
0

私は私のメッセージアプリケーションとしてMailboxerを使っています。Ruby on Railsは別のビューに値を渡します

このサイトについては、すべての分類は、私がnew_messageフォームにルーティングし、受信者の罰金にメッセージを書くために実装した連絡先販売者リンクを持っています!

ここで、連絡フォームが作成されたことに関する情報を追加したいと思います。

class MessagesController < ApplicationController 

    before_action :authenticate_user! 

    def new 
    @user = User.find_by(id: params[:recipient_id]) 
    end 


    def name 
    first_name 
    end 



    def mailboxer_email(object) 
    email 
    end 



    def create 
    recipients = User.find_by(id: params[:recipient_id]) 
    conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation 
    flash[:success] = "Message has been sent!" 
    redirect_to conversation_path(conversation) 
    end 



end 

これは、new_message_pathを呼び出して、受信者を定義するuser_idを取得するビューです。

<div class="center-div"> 
    <div class="col-lg-12" id="image"> 
     <% @classified.photos.each do |p| %> 


     <h3 class="dark-grey"><%= @classified.title %></h3> 
     <h5 class="dark-grey">Loved by: <%= @classified.favorited_by.count %> </h5> 

     <%= image_tag p.image %> 

     <%end%> 

    </div> 


    <%= link_to "", new_message_path(:recipient_id => @classified.user_id), :class => "glyphicon glyphicon-envelope" , :style => "color:#EFCE7B" %> 

    <%if current_user.favorite_classifieds.collect(&:classified_id).include?(@classified.id) %> 
    <%= link_to "", favorite_classified_path(@classified, type: "unfavorite") , :class => "glyphicon glyphicon-heart" , :style => "color:#FF0000", method: :put %> 

    <%else%> 

    <%= link_to "", favorite_classified_path(@classified, type: "favorite") , :class => "glyphicon glyphicon-heart" , :style => "color:#000000", method: :put %> 
    <%end%> 


    <%= link_to "", editlisting_path(@classified) , :class => "glyphicon glyphicon-flag" , :style => "color:#EB573B" %> 
    <ul> 


     <h3 class="dark-grey">Model</h3> 
     <li><%= @classified.model %></li> 
     <h3 class="dark-grey">Description</h3> 
     <li><%= @classified.description %></li> 
     <li><%= link_to "Back", '/' , :class => "link" %></li> 
     <li><%= link_to @classified.user.first_name, profile_path(@classified.user_id) %></li> 


    </ul> 
</div> 

と、これは良い説明は私に多くのことを助ける任意の解決のためにも、私は分類

<div class="center-div"> 
    <div class="messages-box"> 
    <% page_header "Αποστολή μηνύματος" %> 

    <%= form_tag messages_path, method: :post do %> 
     <div class="form-group"> 
     <%= label_tag 'message[subject]', 'Subject' %> 

     <%= text_field_tag 'message[subject]', nil, class: 'form-control', required: true %> 
     </div> 


     <div class="form-group"> 
     <%= label_tag 'message[body]', 'Message' %> 
     <%= text_area_tag 'message[body]', nil, cols: 3, class: 'form-control', required: true %> 
     </div> 

     <div class="form-group"> 

     <%= hidden_field_tag(:recipient_id, "#{@user.id}") %> 
     </div> 

     <%= submit_tag 'Send', class: 'btn btn-primary' %> 
    <% end %> 
    </div> 
</div> 

のデータを持つようにしたい私のmessage_new図です。

ありがとうございます。

答えて

0

ネストされたルートを使用してしまいました。

resources :classifieds do 
     put :favorite, on: :member 
     resources :messages do 
     end 
    end 

この

は、コントローラ

class MessagesController < ApplicationController 

    before_action :authenticate_user! 

    def new 
    @user = User.find_by(id: params[:recipient_id]) 
    @classified = Classified.find_by(id: params[:classified_id]) 
    end 


    def name 
    first_name 
    end 



    def mailboxer_email(object) 
    email 
    end 



    def create 
    classifieds = Classified.find_by(id: params[:classified_id]) 
    recipients = User.find_by(id: params[:recipient_id]) 
    conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation 
    flash[:success] = "Message has been sent!" 
    redirect_to conversation_path(conversation) 
    end 



end 

から値を得て、最後に私messages_newビューでレンダリングされた私の見解

<div class="center-div"> 
    <div class="col-lg-12" id="image"> 
     <% @classified.photos.each do |p| %> 


     <h3 class="dark-grey"><%= @classified.title %></h3> 
     <h5 class="dark-grey">Loved by: <%= @classified.favorited_by.count %> </h5> 

     <%= image_tag p.image %> 

     <%end%> 

    </div> 


    <%= link_to "", new_classified_message_path(:recipient_id => @classified.user_id , :classified_id => @classified.id), :class => "glyphicon glyphicon-envelope" , :style => "color:#EFCE7B" %> 

    <%if current_user.favorite_classifieds.collect(&:classified_id).include?(@classified.id) %> 
    <%= link_to "", favorite_classified_path(@classified, type: "unfavorite") , :class => "glyphicon glyphicon-heart" , :style => "color:#FF0000", method: :put %> 

    <%else%> 

    <%= link_to "", favorite_classified_path(@classified, type: "favorite") , :class => "glyphicon glyphicon-heart" , :style => "color:#000000", method: :put %> 
    <%end%> 


    <%= link_to "", editlisting_path(@classified) , :class => "glyphicon glyphicon-flag" , :style => "color:#EB573B" %> 
    <ul> 


     <h3 class="dark-grey">Model</h3> 
     <li><%= @classified.model %></li> 
     <h3 class="dark-grey">Description</h3> 
     <li><%= @classified.description %></li> 
     <li><%= link_to "Back", '/' , :class => "link" %></li> 
     <li><%= link_to @classified.user.first_name, profile_path(@classified.user_id) %></li> 


    </ul> 
</div> 

ある

<div class="center-div"> 
    <div class="messages-box"> 
    <% page_header "Αποστολή μηνύματος" %> 

    <%= form_tag messages_path, method: :post do %> 
     <div class="form-group"> 
     <%= label_tag 'message[subject]', 'Αποστολή μηνύματος για την αγγελία : ' %> 
     <%= label_tag(:classified_id, "#{@classified.title}") %> <!-- integrate this --> 

     <%= text_field_tag 'message[subject]', nil, class: 'form-control', required: true %> <!-- integrate here --> 
     </div> 


     <div class="form-group"> 
     <%= label_tag 'message[body]', 'Μύνημα στον χρήστη : ' %> 
     <%= label_tag(:recipient_id, "#{@user.first_name}") %> 


     <%= text_area_tag 'message[body]', nil,rows: 15, cols: 3, class: 'form-control', required: true %> <!-- integrate here --> 
     </div> 

     <div class="form-group"> 
     <%= hidden_field_tag(:recipient_id, "#{@user.id}") %> 
     </div> 

     <%= submit_tag 'Send', class: 'btn btn-primary' %> 
    <% end %> 
    </div> 
</div> 
関連する問題