Ruby on Rails将值传递给另一个视图

时间:2016-09-12 15:16:30

标签: ruby-on-rails

我在我的rails应用程序中使用Mailboxer作为我的消息传递宝石。

所以这个网站是关于分类广告的。每个分类都有一个联系卖家链接,我已经实现了路由到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>

这是我的message_new视图,我想要获得已分类的数据

<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>

对于任何解决方案,一个好的解释会帮助我很多。

谢谢。

1 个答案:

答案 0 :(得分:0)

我最终使用了嵌套路线。

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

这是我的观点

<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>

从控制器获得值

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="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>