使用remote =>从rails表单渲染没有布局的内容真正

时间:2013-01-24 00:06:48

标签: ajax ruby-on-rails-3 response

我有一个表单将任务标记为已完成:

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

谢谢大家!

1 个答案:

答案 0 :(得分:2)

你可以添加smth。像格式块一样:

format.json { render :layout => false }