Rails: dynamically changing form content

半城伤御伤魂 提交于 2019-12-13 04:38:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!