【发布时间】: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