根据下拉选择

时间:2017-10-12 03:11:05

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

我有一个名为 Email_Template 的表格。它包含两列:template_name和template_body

我还有一个表单,其中用户在下拉列表中选择template_name (来自数据库列)。在相同的表单中,基于template_name选择, textarea应显示来自同一数据库表的相应“template_body ”内容。请告诉我怎么做。

我的表格

<%= form_for :email_form, :html => {:class => "form-horizontal"}, url: candidates_send_email_path do |f| %>

 <div class="form-group">
   <%= f.label :template_name, "Template" %>
      <div class="col-md-10">
         <%= select_tag "template_name", options_from_collection_for_select(Email_Template.all, "id", "id") %>
      </div>
 </div>

 <div class="form-group">
   <%= f.label :template_body, "Template Body" %>
      <div class="col-md-10">
        <%= f.text_area :email_content, rows: 7 %><br/>
      </div>
 </div>
 <div class=text-right>
   <%= f.button class: "btn btn-primary btn-modal" do %>
     SEND EMAIL <i class="glyphicon glyphicon-send"></i>
   <% end %>
 </div>

<% end %>

的routes.rb

post '/template_body_finder/:template_id' => 'search#find_template'

search_controller.rb

def find_template
    @email_template = Email_Template.find(params[:template_id])
    respond_to do |format|
      format.js
    end
  end

search.js.coffee

$ ->
  $(document).on 'change', '#template_id', (evt) ->
    template_id = $(this).val()
    window.alert(template_id)
    curr_url = "/template_body_finder/" + template_id
       $.ajax({
          type: 'POST',
          url: curr_url,
          data: {'template_id': template_id},
          success: ( data, status, xhr ) -> })

find_template.js.erb

$(“#email_content”).val= <%= @email_template.template_body %>
;

1 个答案:

答案 0 :(得分:0)

这里试试这个: -

1-你的表格

<%= form_for :email_form, :html => {:class => "form-horizontal"}, url: candidates_send_email_path_path do |f| %>

 <div class="form-group">
   <%= f.label :template_name, "Template" %>
      <div class="col-md-10">
         <%= select_tag "template_name", options_from_collection_for_select(Email_Template.all, "id", "id") %>
      </div>
 </div>

 <div class="form-group">
   <%= f.label :template_body, "Template Body" %>
      <div class="col-md-10">
        <%= f.text_area :email_content, rows: 7 %><br/>
      </div>
 </div>
 <div class=text-right>
   <%= f.button class: "btn btn-primary btn-modal" do %>
     SEND EMAIL <i class="glyphicon glyphicon-send"></i>
   <% end %>
 </div>

<% end %>

<script type="text/javascript">
    $('#template_name').on('change', function(){
        var template_id = $(this).val();
        // alert(template_id);
        // ajax request
    $.ajax({
      url: "/template_body_finder",
      type: "GET",
      data : {
        template_id: template_id
      },
      dataType: "script",
    });
    })
</script>

2-更改您的网址:

get '/template_body_finder' => 'search#find_template'
控制器中的

3- -

def find_template
    @email_template = Email_Template.find(params[:template_id])
    respond_to do |format|
      format.js
    end
end
你的find_template.js.erb文件中的

4-它应该是这个 -

$('#email_form_email_content').val("<%=@email_template.template_body%>");

这对我有用,如果您有任何疑问,可以在评论中提出。 谢谢。