mercredi 15 septembre 2021

How can get the selected option id in rails

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

Aucun commentaire:

Enregistrer un commentaire