I am trying to get the id of selected option from dropdown but i get the nill id when i create the project
def create
developer_id = params[:developer_id]
parameters = project_params.merge({ user_id: current_user.id, developer_id: developer_id })
@project = Project.new(parameters)
respond_to do |format|
if @project.save
format.html { redirect_to projects_path, flash: { success: 'Project added successfully ' } }
else
format.html { render :new }
end
end
end
def project_params
params.require(:project).permit(:name, :user_id)
end
new.html.erb
<%= form_for :project, :html => {:class=>"form-group"}, url: projects_path do |f| %>
Add task: <%=f.text_field :name, class:"form-control" %><br>
<h2>Select Developer</h2>
<%= f.select :developer_id, options_for_select(@users.collect {|user|["#{user.name}","#{user.id}"]}) %><br>
<%= f.submit "Add" %>
<% end %>
CodePudding user response:
The developer_id
is nested under project
just like the other params.
developer_id = params[:project][:developer_id]
or
developer_id = params.dig(:project, :developer_id)
When you are unsure where to find certain params or if they even exist then the easiest way is certainly to look into your applications log file. There you should see the incoming request and all the params and how they are nested.