5

テーブルにリードのリストを表示するRailsアプリがあります。列の 1 つで、ドロップダウン メニューにリードのステータスを表示します。ドロップダウンで選択した値を変更すると、リードのこのステータスを変更できるようにしたいと考えています。

これは私が試したものです:

表のセルにフォームを表示するコード:

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

リードコントローラーの私の update_lead_status メソッド:

#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

また、サブミッションはリフレッシュせずにAjaxスタイルにしたいです。

4

1 に答える 1

4

フォーム ID を設定してフォームを送信する

<%= 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 %>
于 2013-04-20T10:32:16.210 に答える