Reputation: 85
I want to create tasks by using ajax with loader. Here is my View -
<%= 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 %>
And here is my controller -
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
Here I write a code to show all tasks
<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>
Here is my create.js.ejb ---
page.replace_html('index' , redirect_to root_url)
When i submit my task then it creates in database but page is not updated. When i again refresh the page then task is showing. I know the problem in create.js.ejb file. How can i show update tasks in my table??
Upvotes: 1
Views: 2726
Reputation: 8169
page.replace_html('index' , redirect_to root_url) // this is invalid
"page.replace_html" no longer works in rails 3
In create.js.erb
alert("record created");
// or update div with id some_div_id
$("#some_div_id").html("<%= escape_javascript(render :partial => 'some_partial') %>");
Upvotes: 0
Reputation: 2774
First of all make sure that you have included jquery and jquery_ujs into your application.js file.
Its a bad habit to redirect from the view. Make it format.js { redirect_to root_url}
And use *.js.erb
template instead of rjs
template.
In create.js.erb
you might do something like this
$("#some_div").append("some HTML content that is related to created record")
Hope this helps
Upvotes: 2