开发者

Drop Down Box - Populated with data from another table in a form - Ruby on Rails

I am attempting to add a note capability to my CRM that I am creating.I am employing an authenticated environment where users can manage their prospects. For instance, I sign up, I login, I can enter prospects and view them. So...I have created a prospects and user table and have them all working great. I am now trying to give the users the ability to take a note and "attach" it to a prospect. So...I have created a notes table with the columns, note & prospect. The user can take a note from wherever they pull this form, and my goal is to have their clients names available in a dropdown box to attach to the form. So far I have created an object in the prospects controller that says the following

def index
    @myprospects = current_user.prospects.find(params[:id])
  end

I am struggling with the next step 开发者_高级运维to create the dropdown in the view/notes/new.html.erb file. The form looks like this:

<h1>New note</h1><hr/>

<% form_for(@note) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :note %><br />
    <%= f.text_area :note %>
  </p>
  <p>
    <%= f.label :prospect %><br />
    <%= f.text_field :prospect %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', notes_path %>

How do I map the

  <p>
    <%= f.label :prospect %><br />
    <%= f.text_field :prospect %>
  </p>

field to display the data I want? I am three days into rails so talk to me like I am a 10 year old (a 10 year old that doesn't know rails).


You're looking for a select box.

Rails provides a lot of helpers to create form inputs. The right one for your job is collection_select.

Assuming you've set up a many-to-one relationship between notes and prospects properly, and the column that contains a prospect's name in the prospects table is called 'name', this will do exactly what you want:

<p>
  <%= f.label :prospect %><br />
  <%= f.collection_select :prospect_id, @myprospects, :id, :name, :prompt => "Select a prospect" %>
</p>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜