admin管理员组

文章数量:1336631

I have a Rails app with listing of leads in a table. In one of the collumns I display status of a lead in a drop down menu. I want to enable changing this status of the lead on changing the value selected in the drop down.

This is what I tried:

The code to display the form in 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>

my update_lead_status method in leads controller:

#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

Also I want the submission to be Ajax style without refreshing.

I have a Rails app with listing of leads in a table. In one of the collumns I display status of a lead in a drop down menu. I want to enable changing this status of the lead on changing the value selected in the drop down.

This is what I tried:

The code to display the form in 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>

my update_lead_status method in leads controller:

#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

Also I want the submission to be Ajax style without refreshing.

Share Improve this question asked Apr 20, 2013 at 10:07 sleeping_dragonsleeping_dragon 7111 gold badge10 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Set form id and then submit form

<%= form_for(lead,:url => 'update_lead_status',:html=>{:id=>'lead_form'}) do |f| %>

 <%= f.select :status, ["to_call","called","confirmed","lite"], :selected => lead.status, onchange: "$('#lead_form').submit();" %>
<% end %>

本文标签: javascriptSubmit Rails from on change of selected value in dropdown formStack Overflow