1

我想通过将 ajax 与 loader 一起使用来创建任务。这是我的观点-

<%= form_for(@task, :remote => "true") do |f| %>
<center><h3>Organize Your Tasks</h3></center>
<table class="table table-bordered table-condensed table-striped">
<tr>
<td> 
     <%= f.select :taskcategory, options_for_select(["Money/Career", "Relationship", "Safety", "Health Care", "God", "Hobbies", "Society"]), {}, {:multiple => false} %>
    </td>
    <td style="width:120px;">
   <%= f.text_field :taskname, :placeholder => "Task Name" %>
</td>
<td>
  <%= f.select :importance, options_for_select(["Highly Important", "Important", "Less Important"]), {}, {:multiple => false} %></center>           
    </td>   
<td  style="width:120px;">
       <%= f.text_field :startdate , :id => "from", :placeholder => "Start Date", :value => "#{Date.today.strftime("%d/%m/%Y") }" %>   
    </td>   
<td  style="width:120px;">
      <%= f.text_field :targetdate , :id => "to", :placeholder => "End Date", :disabled => "ture"%>   
     </td>
    <td>                     
     <%= f.submit "Create", class: "btn"%> 
    </td>                
   </tr>     
</table>  
<% end %>

这是我的控制器 -

def create
  @task= current_user.tasks.build(params[:task])   
  respond_to do |format|
    if @task.save 
      flash[:success] = "Task created!"
      format.html {redirect_to root_url }
      format.js { redirect_to}
    else         
      format.html { render 'static_pages/index'}
    end
  end
end

在这里我写了一个代码来显示所有的任务

<div class="tabbable" style="margin-left:-50px" >
   <ul class="nav nav-tabs span10" id="myTab" style="margin-left:50px">
     <li class="active"><a href="#tab1">All Tasks(<%=   current_user.tasks.where(:status => 'active').count %>)</a></li>
     <li ><a href="#tab2">Dreams(<%= current_user.tasks.where(:importance => 'Highly Important', :status => 'active').count %>)</a></li>
     <li ><a href="#tab3" >Today's Tasks(<%= current_user.tasks.where(:targetdate => Date.today.to_s, :status => 'active').count %>)</a></li>
     <li ><a href="#tab4" >Imp Tasks(<%= current_user.tasks.where(:importance => 'Important', :status => 'active').count %>)</a></li>
     <li id="fat-menu" class="dropdown aa">
     <a href="#" class="dropdown-toggle" data-toggle="dropdown">Others</a>
     <ul class="dropdown-menu">
    <li><a href="#tab5" >Postpone(<%= current_user.tasks.where(:status => 'postpone').count %>)</a></li>
    <li><a href="#tab6" >Satisfied(<%= current_user.tasks.where(:status => 'satisfied').count%>)</a></li>
    <li><a href="#tab7" >Unsatisfied(<%= current_user.tasks.where(:status => 'unsatisfied').count%>)</a></li>
    <li><a href="#tab8" >Closed(<%= current_user.tasks.where(:status => 'closed').count%>)</a></li>
     </ul>
  </li>
</ul>
 <div class="tab-content span10" >
  <div class="tab-pane active" id="tab1">    
<%= render 'shared/feed' %> 
  </div>
  <div class="tab-pane" id="tab2">
   <%= render 'shared/feed_dream' %> 
  </div>
  <div class="tab-pane" id="tab3">
     <%= render 'shared/feed_urgent' %> 
  </div>
  <div class="tab-pane" id="tab4">
     <%= render 'shared/feed_important' %> 
  </div>
  <div class="tab-pane" id="tab5">
     <%= render 'shared/feed_postpone' %> 
  </div>
  <div class="tab-pane" id="tab6">
    <%= render 'shared/feed_satisfied' %> 
  </div>
  <div class="tab-pane" id="tab7">
     <%= render 'shared/feed_unsatisfied' %> 
  </div>
  <div class="tab-pane" id="tab8">
    <%= render 'shared/feed_closed' %> 
  </div>
</div>

这是我的 create.js.ejb ---

page.replace_html('index' , redirect_to root_url)

当我提交我的任务时,它会在数据库中创建但页面没有更新。当我再次刷新页面时,就会显示任务。我知道 create.js.ejb 文件中的问题。如何在我的表中显示更新任务?

4

2 回答 2

2

首先确保您已将 jquery 和 jquery_ujs 包含到您的 application.js 文件中。

从视图重定向是一个坏习惯。做了format.js { redirect_to root_url}

并使用*.js.erb模板代替rjs模板。

create.js.erb你可能会做这样的事情

$("#some_div").append("some HTML content that is related to created record")

希望这可以帮助

于 2013-02-14T07:18:00.967 回答
0
page.replace_html('index' , redirect_to root_url) // this is invalid

“page.replace_html”不再适用于 rails 3

在 create.js.erb

alert("record created");
// or update div with id some_div_id
$("#some_div_id").html("<%= escape_javascript(render :partial => 'some_partial') %>");
于 2013-02-14T08:45:27.287 回答