将对象从控制器传递到模态视图rails

时间:2015-03-27 14:59:08

标签: javascript jquery ruby-on-rails ruby ruby-on-rails-4

我想将一个对象从我的控制器传递到我的模态视图。

在我的index.html.erb

<div class="modal fade bs-example-modal-lg" id="email-template-user-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-lg">
        <div class="modal-content">
          <%= render "shared/email_template_user", :locals => { :application => @applicant }%>
        </div>
      </div>
    </div>

     <%= link_to application.loan_application.email, lp_banker_assignments_get_email_status_path(:applicant_id => application.loan_application.id), class: 'email-template-user-modal', id: "email-template-user-id", :data => { :toggle => "modal", :target => "#email-template-user-modal"} %>

在我的controller.rb

def get_email_status
    @applicant = LoanApplication.where(:id => params[:applicant_id])

    respond_to do |format|
      format.json
    end   
  end

在我的模态视图文件_email_template_user.html.erb

<div class="panel panel-default">
  <div class="panel-heading">
    <h4 class="panel-title">Compose Email</h4>
    **<%= ap @applicant %>**
  </div>
  <div class="panel-body">
    <%= form_tag :action => 'email_status', class: 'form-horizontal' do %> 
        <div class="">
          <div class="col-md-3">Recipient</div>
          <div class="col-md-9">
            <%= email_field_tag "recipient", "", class: " string optional form-control"%>
          </div>
        </div>
      </div>
     </div>

如果我在我的模态文件中调用@applicant,它会在我的cmd日志中返回nil,我发现我收到有关Missing Template lp_banker_assignments/get_email_status, application/get_email_status with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee, :slim]}. Searched in:的错误

如何让我的对象在我的模态视图中显示?

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

您可以在相应的视图文件夹中创建get_email_status.js.erb文件,因为您的控制器操作在响应块中需要json。希望它有效。