Rails:动态更改表单内容 [英] Rails: dynamically changing form content
问题描述
我正在Rails中构建消息应用程序,用户可以在其中从模板发送消息.我有一个模板数据库,用户可以根据类别选择模板.
I am building message app in rails where user can send message from templates. I have a database of templates, and the user can select templates as per category.
在我的消息模型中,我想根据所选类别动态呈现模板.我在Google上查找了示例,但找不到相关的解决方案.
In my message model, I want to render the template dynamically based on category selected. I looked for examples on google, but was not able to find a relevant solution.
这是我的留言表格:
<%= form_for @message, :html => {:multipart => true} do |m| %>
<%= m.select :biz_case, options_for_select(Message::Bcase), :prompt => "Select business case" %>
<%= m.text_field :subject, :class => "message-text", :placeholder => "Subject" %>
<div class="message-body">
<%= m.text_area :message, :class => "message-body", :class => "redactor", :placeholder => "Your content" %>
</div>
<%= m.select :user_type, options_for_select(Customer::CType), :prompt => "Customer segment" %>
<%= m.submit %>
<% end %>
在上面的表格中,我希望根据所选的业务案例显示主题和正文.像这样:
In the above form, I am looking to display the subject and body based on the selected business case. Something like:
if biz_case == "promote"
subject = @template.subject where ("biz_case = ?", "promote")
message = @template.content where ("biz_case = ?", "promote")
end
主题和消息将显示在输入文本字段中.
The subject and message would be displayed in input text fields.
谁能告诉我该怎么做?
推荐答案
在您的方法中:
@subject = @template.subject where ("biz_case = ?", "promote")
视图中:
<%= m.text_field :subject, :value => @subject, :class => "message-text", :placeholder => "Subject" %>
这篇关于Rails:动态更改表单内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!