我得到了一个表格来标记任务已完成:
<%= simple_form_for task, :html => { :class => "check-task", :id => ("task" + task.id.to_s) }, :method => :put, :remote => true do |f| %>
<% task_class = ("#task" + task.id.to_s) %>
<%= f.check_box :completed, :onclick => "$('#{task_class}').submit()" %>
<% end %>
所以我在点击复选框时通过 js/ajax 提交此表单。
响应数据现在包括整个页面以及<head>
和所有内容:
respond_to do |format|
if @task.update_attributes(params[:task])
format.html { redirect_to @task, notice: 'Task was successfully updated.' }
format.js { redirect_to request.referer, notice: 'Task was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
那么,怎么可能只是取回相应部分的东西呢?
看这里:
<%#= link_to 'New Task', new_task_path, :class => "pull-right" %>
<div class="clearfix"></div>
<div class="row">
<div class="span6 sidebar">
<ul class="nav nav-pills nav-stacked">
<% for stock_list in @stock_lists %>
<%= navigation_li_link stock_list.title, list_tasks_path(stock_list) %>
<% end %>
<% for user_list in @user_lists %>
<%= navigation_li_link user_list.title, list_tasks_path(user_list) %>
<% end %>
</ul>
</div>
<div class="span18 content">
<% unless @tasks.empty? %>
<div class="row">
<% @tasks.each do |task| %>
<div class="span18 task<%= task.state %>">
<div class="row">
<div class="span1">
<%= simple_form_for task, :html => { :class => "check-task", :id => ("task" + task.id.to_s) }, :method => :put, :remote => true do |f| %>
<% task_class = ("#task" + task.id.to_s) %>
<%= f.check_box :completed, :onclick => "$('#{task_class}').submit()" %>
<% end %>
</div>
<div class="span9"><%= task.content %></div>
<div class="span5"><%= task.due_at %></div>
<div class="span3">
<%= link_to '<i class="icon-edit"></i>'.html_safe, edit_task_path(task) %>
<%= link_to '<i class="icon-trash"></i>'.html_safe, task, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
</div>
</div>
<% end %>
</div>
<% else %>
<p class="lead">No Task in here</p>
<% end%>
</div>
</div>
谢谢大家!
您可以在格式块中添加 smth。
format.json { render :layout => false }