问题
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.
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.
This is my message form:
<%= 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.
Can anyone tell me how to do this?
回答1:
in your method:
@subject = @template.subject where ("biz_case = ?", "promote")
in view:
<%= m.text_field :subject, :value => @subject, :class => "message-text", :placeholder => "Subject" %>
来源:https://stackoverflow.com/questions/17525724/rails-dynamically-changing-form-content