I am working on a Rails application that has a table of leads. Within one of the columns, I display the lead's status in a drop-down menu. My goal is to allow users to change the lead's status by simply selecting a different value from the drop-down menu.
Here is my current approach:
The following code snippet is used to display the form within a table cell:
<% @leads.each do |lead| %>
<tr>
<td><%= lead.id %></td>
<td><%= form_for(lead,:url => 'update_lead_status') do |f| %>
<div class="field">
<%= f.select :status, ["to_call","called","confirmed","lite"], :selected => lead.status, onchange: "this.form.submit();" %>
</div>
<% end %>
</td>
Within my leads controller, here is the update_lead_status method:
#PUT
def update_lead_status
@lead = Lead.find(params[:id])
respond_to do |format|
# format.js
if @lead.update_attributes(params[:lead])
format.html { redirect_to leads_url, notice: 'Lead was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @lead.errors, status: :unprocessable_entity }
end
end
end
Additionally, I am looking to implement Ajax submission for a smoother user experience without page refreshing.