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

I am trying to add a notes function to my CRM that I am creating. I use an authenticated environment in which users can manage their prospects. For example, I sign up, I log in, I can enter perspectives and view them. So ... I created a perspective and user table, and they all work great. Now I'm trying to give users the opportunity to take a note and “attach” it to the perspective. So ... I created a note table with columns, note and perspective. The user can take a note wherever they pull this form, and my goal is to make their customer names available in the drop-down list to attach to the form. So far I have created an object in the perspective controller that says the following

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

I am struggling with the next step to create a drop-down list in the file view / notes / new.html.erb. The form is as follows:

 <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 display

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

to display the data I want? I’m on the rails for three days, so talk to me as if I’m 10 years old (10 years old man who does not know the rails).

+3
source share
1 answer

You are looking for a field of choice.

Rails provides many helpers for creating form inputs. Correct for your work collection_select .

Assuming that you have correctly established the relationship between notes and perspectives, and the column containing the name of the perspective in the table “Perspectives” 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> 
+10
source

All Articles