我想通过使用带加载器的ajax来创建任务。 这是我的观点 -
<%= 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文件中的问题。如何在表格中显示更新任务?
答案 0 :(得分: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")
希望这有帮助
答案 1 :(得分: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') %>");