在视图中显示选择值的选项(rails)

时间:2014-12-23 02:16:58

标签: ruby-on-rails ruby

我有一个简单的表单,带有一个select标签,带有select选项。我希望能够在视图上列出所选的选项。我知道如何执行此操作的唯一方法是调用其中包含ActionItem.new(action_item_params)的实例变量,并使用属性作为方法(在本例中为:purpose)。在视图中,这不显示任何内容,我希望它显示用户选择的选择标记选项。 - OOP,Ruby和Rails的新手 - 可能是新手的情况。在此期间会读到面对面。

表格:

<div align="center"><h1><%= @client_name.name %></h1></div></br></br>

<div align="center"><strong>Communication Options:</strong></br></br>

<%= form_for(:action_item, url: {controller: "action_item", action: "create"} ) do |f| %>

<%= f.label("Purpose Of This Contact?:") %>
<%= select_tag(:purpose, options_for_select([['Initial conversation/pitch', 1],  ['Follow up correspondence', 2], ['Additional selling point', 3], ['New benefits to  convey', 4]])) %></br></br>


<%= f.label("Method Of Correspondence:") %>
<%= select_tag(:correspondence_method, options_for_select([['Send an email', 1],  ['Tweet on Twitter', 2], ['Make a phone call', 3], ['Send direct mail', 4]])) %></br></br>


<%= f.label("Do I know the contact person? ") %>
<%= select_tag(:know_person, options_for_select([['Yes', 1], ['No (have to find out)', 2]])) %></br></br>

<%= f.label("this field will be added, ONLY if above field is NO: How could you find the contact's name?") %></br> 
<%= f.text_field(:contact_name_answer, :size => 50) %></br></br>

<%= f.label("Additional notes") %></br> 
<%= f.text_field(:additional_notes, :size => 50) %></br></br>
<%= f.submit("Store Action Item") %></br></br>



<% end %>

显示页面:

<h1>Action Item: </h1>

Added:  <strong><%= @action_submission.additional_notes %></strong>, nice job.</br> </br>
Added: <strong><%= @action_submission.contact_name_answer %></strong></br></br>
Added: <strong><%= @action_submission.correspondence_method%></strong></br></br>

2 个答案:

答案 0 :(得分:1)

我实际上找到了答案:只使用1个数组,而不是数组数组。现在,数组看起来像:

<%= f.select(:purpose, ['Initial conversation/pitch', 'Follow up correspondence', 'Additional selling point', 'New benefits to convey']) %></br></br>

这很好用。无论如何,感谢阅读!

答案 1 :(得分:0)

尝试使用f.select代替select_tag