我有一個名爲hot.html.erb的視圖,列出了名爲「issues」的項目。我希望用戶能夠簡單地更改選項值,並立即使用AJAX提交更改以更新數據庫中的「狀態」等內容。無法在Rails 3上使用AJAX編輯屬性
發生了什麼事情是,當選擇該選項時,數據庫被更新,但我被重定向到show.html.erb視圖。我想留在hot.html.erb視圖中。我有一種感覺,我不瞭解format.js條目以及需要做什麼。任何幫助,將不勝感激。
我正在使用jquery,query_ujs,rails.js。
我已實現了以下update.html.erb在我IssuesController:
def update
@issue = Issue.find(params[:id])
respond_to do |format|
if @issue.update_attributes(params[:issue])
format.html { redirect_to(@issue, :notice => 'Issue was successfully updated.') }
format.xml { head :ok }
format.js { render :nothing => true }
else
format.html { render :action => "edit" }
format.xml { render :xml => @issue.errors, :status => :unprocessable_entity }
end
end
end
這裏是我的索引頁(hot.html.erb)用選擇字段,我希望用戶表單能夠改變'狀態'和'優先':
<% @issues.each do |issue| %>
<tr>
<td><%= issue.description %></td>
<td><%= issue.notes %></td>
<%= form_for(issue, :remote => true) do |f| %>
<td><%= f.select :status, [["Emerging", "emerging"], ["Investigating", "investigating"], ["Scheduled for Update", "Scheduled for Update"]], {}, :onchange => 'this.form.submit();' %>
</td>
<td><%= f.select :priority, [["Thermonuclear", "nuclear"],["Hot", "hot"], ["Warm", "warm"], ["Cool", "cool"]], {}, :onchange => 'this.form.submit();' %>
</td>
<% end %>
<td><%= issue.case %></td>
<td><%= issue.location %></td>
<td><%= issue.customer.account_name %></td>
<td><%= link_to 'Show', issue %></td>
<td><%= link_to 'Edit', edit_issue_path(issue) %></td>
<td><%= link_to 'Destroy', issue, :confirm => 'Are you sure?', :method => :delete %> </td>
</tr>
<% end %>