【问题标题】:Dynamically update rails form textarea based on dropdown select基于下拉选择动态更新rails form textarea
【发布时间】:2017-10-12 03:11:04
【问题描述】:

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

我还有一个表单,其中用户在下拉列表中选择模板名称(来自数据库列)。在同一个表单中,基于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 %>
;

【问题讨论】:

  • 在你的 find_template.js.erb 中试试这个 $(“#email_content”).val();
  • @StephenM:试过这个,但我认为我的 ajax 调用不起作用。在 find_template.js.erb 中,即使我放入 window.alert,也没有任何反应。所以,对控制器的调用没有发生。对此有何想法?
  • email_templates 是您的模型。然后把这个Email_Template.find(params[:template_id])改成EmailTemplate.find(params[:template_id])

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


【解决方案1】:

这里试试这个:-

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

4- 在你的 find_template.js.erb 文件中应该是这样的-

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

这对我有用,如果您有任何疑问,可以在 cmets 中询问。 谢谢。

【讨论】:

  • 非常感谢@Anand。虽然您的方法适用于所有情况,但当 template_body 有文本后跟“ENTER”时它会失败。例如#.....在这种情况下,更改下拉菜单不会发生任何事情。
  • 你能试试这个吗:find_template.js.erb$('#email_form_email_content').val("&lt;%=@email_template.template_body.html_safe%&gt;");
猜你喜欢
  • 2022-01-05
  • 2014-08-16
  • 1970-01-01
  • 2023-03-05
  • 2020-01-09
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
  • 2013-04-01
相关资源
最近更新 更多