Rails: How do I re-render a partial view on a page? - ruby-on-rails

I have a webpage with a partial view that has a button and displays some information. When the user clicks the button in this partial view, I need to be able to update that partial view from the controller within the action that gets called. Specifically, there is a string that I want to update. Here is my code so far, but it's not working..
partial view (_printLabel.rhtml)
<% if #printAsinEnabled %>
<%= check_box_tag(:needPrint, :needPrint, false,
:onchange => "if (this.checked) $('printLabelAction').show(); else $('printLabelAction').hide();") %>
<%=_('Need to print label?')%><br /><br />
<div id="printLabelAction" class="action" style="display:none;">
<% onPrintLabelClick = generateAjaxRequest(
{:controller => #controller.controller_name,
:action => 'processPrintLabelRequest',
:creturnWipItemId => creturnWipItemId,
:barcode => collectedItemData.fnsku,
:title => asinInfo["title"]},
["parameters: Form.serializeElements([$('printerAddress'),$('printerRemote_true'),$('printerRemote_false')])"]) %>
<%=_('Printing')%>: <%= collectedItemData.fnsku %> <br /> <br />
<%= radio_button_tag(:printerRemote, :false, !session[:printRemote]) %>
<label for="printerRemote_false"><%=_('Local Printer')%></label> <br />
<%= radio_button_tag(:printerRemote, :true, session[:printRemote]) %>
<label for="printerRemote_true"><%=_('Remote Printer')%> - <%=_('Printer Address')%>:</label>
<%= text_field_tag(:printerAddress, session[:printerAddress],
:onchange => onPrintLabelClick,
:onclick => "$('printerRemote_true').checked = true")%> <br />
<div style="text-align:right;">
<%= button_to_function(_("Print Label"), onPrintLabelClick) %>
</div>
<%=_('Error?')%>: <%= errorMessage %> <br /> <br />
</div>
Here is the div where it gets rendered originally:
<div id="Block_asinSummary" class="block">
...
...
<td height="200px" width ="70%">
<%= link_to asinInfo["title"], asinInfo["link"], :target => "_blank" %><br/><br/>
<% if !asinInfo["clothingSize"].nil? %>
<b><%= _('Size')%>:&nbsp</b> <%= asinInfo["clothingSize"]%><br>
<% end %>
<font size='1'>
<% sku = collectedItemData.displaySku %>
<% if sku != collectedItemData.asin%>
<b>SKU:</b> <%= sku %><br>
<% else %>
<b>ASIN:</b> <%= collectedItemData.asin %>
<% end %>
<%= render(:partial => 'printLabel', :locals => { :creturnWipItemId => creturnWipItemId, :collectedItemData => collectedItemData, :asinInfo => asinInfo, :errorMessage => "HI" })%>
</font>
</td>
</tr>
</table>
...
...
<% end %>
And here is the controller code that is supposed to update "errorMessage" with a new string when clicked:
render(:partial => 'printLabel', :layout => false, :locals => { :creturnWipItemId => creturnWipItemId, :collectedItemData => #collectedItemData, :asinInfo => asinInfo, :errorMessage => "Bye" })
I am not seeing any errors from this, but the errorMessage string is not updating from "HI" to "Bye".

You can't call render partial from your controller. Instead, render the template and pass a different error message to the partial using an instance variable.

Related

Form button not responding and badly aligned [Refactoring Ruby on Rails]

I'm working on an assignment for a course I'm doing on refactoring some version of the typo blog. I need to add a new form to a page in order to get some information, the problem is that the submit button is not responding and the form is also badly alligned. http://i.imgur.com/5HKMG0L.png
I don't have a good understanding of front-end, so this app is quite confusing for me but this is the way the page is rendered:
**first view that gets called:**
<% #page_heading = _('New article') %>
<%= render "admin/shared/edit", { :form_type => "article", :form_action => { :action => "new", :id => #article.id , :class => ('autosave')} } %>
**admin/shared/edit:**
<% className = form_action.delete(:class) %>
<%= form_tag(form_action, :id => "#{form_type}_form", :enctype => "multipart/form-data", :class => className) do %>
<%= render :partial => "form" %>
<% end %>
[CODE I ADDED TO CREATE A NEW FORM]
***<h3> Merge Articles </h3>
<div class='clearfix'>
<%= form_tag(categories_path, :class => className) do %>
<%= label :article, :merge_with, 'Article ID' %>
<%= text_field :merge_with , '', {:class => 'span1'}%>
<% end %>
<%= submit_tag 'Merge' %>
</div>***
**_form.html.erb:**
<input type="hidden" name="current_editor" id="current_editor" value="<%= current_user.editor %>" />
<input type="hidden" name="user_textfilter" id="user_textfilter" value="<%= current_user.text_filter_name %>" />
<div id="autosave"></div>
<div id="article_keywords_auto_complete" class="auto_complete"></div>
<%= error_messages_for 'article' %>
<div class='editor-right'>
<div class='well'>
<h4><%= _("Publish settings") %></h4>
<div class='actions'>
<%= link_to_destroy_with_profiles(#article) unless #article.id.nil? or #article.state.to_s.downcase == 'draft' %>
<span id='destroy_link'><%= link_to_destroy_draft #article %></span>
<span id='preview_link'><%= link_to(_("Preview"), {:controller => '/articles', :action => 'preview', :id => #article.id}, {:target => 'new', :class => 'btn info'}) if #article.id %></span>
</div>
<div class='clearfix'>
<%= _("Status:") %> <strong><%= #article.state.to_s.downcase %></strong> Change
<ul class='inputs-list'>
<li id='status' style='display: none;'>
<label for="article_published">
<%= check_box 'article', 'published' %>
<%= _("Published") %>
</label>
</li>
</ul>
</div>
<div class='clearfix'>
Comments are <strong>enabled</strong> and trackbacks are <strong>disabled</strong> Change
<ul class='inputs-list' id='conversation' style='display: none'>
<li>
<label for="article_allow_pings">
<%= check_box 'article', 'allow_pings' %>
<%= _("Allow trackbacks") %>
</label>
</li>
<li>
<label for="article_allow_comments">
<%= check_box 'article', 'allow_comments' %>
<%= _("Allow comments") %>
</label>
</li>
</ul>
</div>
<div class='clearfix'>
<%= _("Publish <strong>now</strong>") %> Change
<ul class='inputs-list'>
<li id='publish' style='display: none;'>
<%= calendar_date_select 'article', 'published_at', {:class => 'span3'} %>
</li>
</ul>
</div>
<div class='clearfix'>
<%= _("Visibility:") %> <strong>public</strong> Change
<ul class='inputs-list' id='visibility' style='display: none'>
<li>
<label for="article_password"><%= _("Password:") %>
<%= password_field :article, :password, :class => 'span3' %>
</label>
</li>
</ul>
</div>
<div class='clearfix'>
<%= _("Permalink:") %> Change
<ul class='inputs-list' id='permalink' style='display: none'>
<li>
<%= text_field 'article', 'permalink', {:class => 'span4'} %>
</li>
</ul>
</div>
<div class='clearfix'>
<%= _("Article filter") %>: <strong><%= #article.text_filter.description %></strong> Change
<ul id='text_filter' class='inputs-list' style='display: none'>
<li>
<select name="article[text_filter]" id="text_filter">
<%= options_for_select text_filter_options, #article.text_filter %>
</select>
</li>
</ul>
</div>
<div class='actions'>
<input id='save_draft' type="submit" value="<%= _('Save as draft') %>" name="article[draft]" class='btn info' />
<%= save( _("Publish")) %>
</div>
</div>
<div class='well'>
<h4><%= _("Categories") %></h4>
<%= render 'categories' %>
</div>
<%= get_post_types %>
</div>
<div class='editor-left'>
<div>
<div>
<%= text_field 'article', 'title', :class => 'span1', :style => ' width: 99%', :placeholder => _('Title') %>
</div>
<%= render('images', { :images => #images}) unless #images.empty? %>
<div id='editor-menu'>
<ul class="tabs">
<li id="f" class='<%= "active" if current_user.editor == 'visual' %>'>
<%= build_editor_link("Visual", 'insert_editor', 'fck', 'visual_editor', 'visual') %>
</li>
<li id="s" class='<%= "active" if current_user.editor == 'simple' %> '>
<%= build_editor_link("HTML", 'insert_editor', 'simple', 'simple_editor', 'simple') %>
</li>
</ul>
</div>
<div id="editor">
<div id='quicktags' style='<%= "display: none;" if current_user.editor == 'visual' %>'>
<script type="text/javascript">edToolbar('article_body_and_extended');</script>
</div>
<div id ='visual_editor' <%= "style='display: none;'" if current_user.editor == 'simple'%> >
<%= ckeditor_textarea('article', 'body_and_extended', {:class => 'large', :height => '300px', :rows => '20'}) if current_user.editor == 'visual' %>
</div>
<div id='simple_editor' class='input_text' <%= "style='display: none;'" if current_user.editor == 'visual'%> >
<%= text_area('article', 'body_and_extended', {:class => 'large', :height => '300px', :rows => '20'}) if current_user.editor == 'simple' %>
<%= render_macros(#macros) if current_user.editor == 'simple' %>
</div>
</div>
<h4><%= _("Tags") %></h4>
<div class='class'>
<%= text_field 'article', 'keywords', {:autocomplete => 'off', :style => 'width: 100%'} %>
</div>
<%= auto_complete_field 'article_keywords', { :url => { :action => "auto_complete_for_article_keywords"}, :tokens => ','}%>
</div>
<div class='separator'>
<h4><%= _("Excerpt") %></h4>
<div class=''>
<%= text_area 'article', 'excerpt', {:height => '150', :style => 'width: 100%', :rows => '5'} %>
<span class='help-block'><%=_("Excerpts are posts summaries that are shown on your blog homepage only but won’t appear on the post itself") %></span>
</div>
</div>
<div class=''>
<h4><%= _("Uploads") %></h4>
<p class='help-block'>Uploads will be displayed as attachments in your RSS feed, but won't appear in your articles.</p>
<ul id='attachments' class='inputs-list'>
<%= render 'admin/content/attachment', { :attachment_num => 1, :hidden => false } -%>
</ul>
</div>
</div>
</div>
Any help would be really appreciated. Thanks.
You need to use CSS to align your form to the left. The best way to do this is going into app/assets/articles.css (or any of your CSS files) and adding this line:
.left { text-align: left; }
Then go back into your views and assign anything you want to align to the left the "left" class, for example in your form you can wrap the relevant portions in a div to make everything align left:
<div class="left">
<%= form_tag(categories_path, :class => className) do %>
<%= label :article, :merge_with, 'Article ID' %>
<%= text_field :merge_with , '', {:class => 'span1'}%>
<% end %>
<%= submit_tag 'Merge' %>
</div>

getting textfield value and search result after page after click on search button in ruby on rails

In my search page I have a textfield of zip code and when I enter Zip code in the textfield and click on search button the value of text field is loss and also search result is loss, what I do to stable textfield value of zip code and search result after click on search button. below is my view page:
<%= form_for :search, :url => { :method => :get, :action => :search } do |f| %>
<table width="100%">
<tr>
<td align="center" style="vertical-align:top;" width="35%">
<table>
<tr>
<td align="center">
<%= text_field_tag "tf_Zip",nil,:placeholder =>'Zip' %>
</td>
</tr>
</table>
<table>
<% if !#user.nil? %>
<% #user.each do |uzr| %>
<tr>
<td bgcolor="#CCDBE0" width="40%">
<%= image_tag #user_img.photo.url(:small),:alt => " " %>
<br/>
<div><a style="color:blue;" href = 'profile?id=<%= uzr.id %>'><%= uzr.First_Name + " " + uzr.Last_Name %></a></div>
</td>
<td bgcolor="#CCDBE0" width="60%" style="text-align:center;">
<div class='buttonblck1'><a href='searchings?id=<%= uzr.id %>'>Send Request </a></div>
</td>
</tr>
<% end %>
<% end %>
</table>
<% end %>
and below is controller page
if ( !params[:tf_Zip].blank? or params[:tf_Zip] != "" )
#user = User.find(:all,:conditions=>['"user_Zip" = ? and "id" != ? ',params[:tf_Zip], current_user.id])
end
Kindly suggest me, waiting for your reply.'
Thanks
First:
form_for is meant to be used with an instance of ActiveModel or ActiveRecord.
Use form_tag, if you want to create a form, that is not based on an object.
Second:
as you have not object, where the attributes are stored, you have to provide them yourself.
The value is defined in the second parameter of text_field_tag
i.e. in your controller:
#zip_search = params[:tf_Zip].presence
if #zip_search
#user = ...
end
and in your view:
<%= form_tag url_for( :controller => :search, :action => :search), :method => :get) do %>
<%= text_field_tag "tf_Zip", #zip_search, :placholder => 'Zip' %>
<% end %>

if !checklogin? then return end

I would like to make show.html.erb(groups) pages open to visitors(,which are not users) as well.
But my code has "if !checklogin? then return end" on groups_controller, so they can't see the show.html.erb pages. I know I should remove "if ! checklogin? then return end" but have no idea how to reconstruct my code as a whole.
Could you give me some hints?
☆members_controller
def checklogin?
if session[:user_id] != nil then
return true
else
redirect_to '/members/login'
return false
end
end
☆login.html.erb(members_controller)
<div class="span4">
<h4 class="title">Users' reading texts</h4>
<% #books.each do |book| %>
<%= image_tag book.imageurl, :width => '30px', :height => '30px'%>
<% end %>
</div><!--span4-->
☆groups_controller
def show
#group = Group.find(params[:id])
#group_message = Group.find(params[:id]).group_messages.build
if !checklogin? then return end
#group_messages = Group.find(params[:id]).group_messages.order("created_at desc")
#me = me?
#member = Member.find(session[:user_id])
#isGr = GroupInMember.where(:member_id => session[:user_id], :group_id =>
params[:id].to_i).count > 0
#gms = Group.find(params[:id]).group_messages.order("created_at desc").scoped
if params[:page].present?
#gms = #gms.where("page = ?" , params[:page] )
end
if params[:content].present?
#gms = #gms.where("content like ?" , "%" + params[:content] + "%")
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #group_messages }
end
end
☆show.html.erb(groups)
<div class="container-fluid">
<div class="row-fluid">
<div class="span4">
<div id="individual">
<h4>※「<%= #group.name %>」のページ</h4>
<%= link_to image_tag(#group.imageurl, :width=>"100" ,:height=>"150"),#group.detailurl , :target => '_blank' %>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>プロフィール</th>
</tr>
</thead>
<tbody>
<tr>
<th>著者</th>
<td><%= #group.author %></td>
</tr>
<tr>
<th>出版年</th>
<td><%= #group.published %></td>
</tr>
<tr>
<th>出版社</th>
<td><%= #group.publish %></td>
</tr>
<tr>
<th>ページ数</th>
<td><%= #group.page %></td>
</tr>
<tr>
<th>ISBN</th>
<td><%= #group.isbn %></td>
</tr>
</tbody>
</table>
</div><!--individual-->
<%# if #group.admin %>
<%#= link_to 'Profile Edit', edit_group_path(#group),class: "btn btn-midium btn-primary" %>
<%# end %>
<hr>
<% if session[:user_id]%>
<div class="group-side">
<% if #isGr %>
※登録済みです。
<% else %>
※登録していません。
<% end %>
</div>
<p class="group-side">
<%= link_to '本棚登録/解除', {:controller => 'groups', :action => 'join', :id =>
#group.id }, class: "btn btn-midium btn-primary"%></p>
</p>
<p></p>
<div class="group-side2"><b>この本を登録した人一覧:</b></div>
<% #group.group_in_members.each do |m| %>
<% #member = Member.find(m.member_id) %>
<% if #member.provider %>
<%= image_tag #member.image, :width =>'30px', :height =>'30px' %>
<% elsif #member.avatar_file_name %>
<%= image_tag #member.avatar.url(:thumb), :width =>'30px', :height => '30px' %>
<% else %>
<%= image_tag 'love.png', :width =>'30px', :height => '30px' %>
<% end %>
<%# if GroupInMember.where( :member_id => session[:user_id], :group_id => #group.id ).length > 0 %>
<%# end %>
<% end %>
</div>
<% end %>
<div class="span8">
<p>
<b></b>
</p>
<div class="post_on_group">
<% if GroupInMember.where(:member_id =>session[:user_id], :group_id=>#group.id).length > 0 %>
<%= form_for([#group, #group_message]) do |f| %>
<% if #group_message.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#group_message.errors.count, "error") %> prohibited this group_message from being saved:</h2>
<ul>
<% #group_message.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%#= f.label :member_id %>
<%= f.hidden_field :member_id, :value => session[:user_id] %>
</div>
<div class="field">
<%#= f.label :group_id %>
<%= f.hidden_field :group_id %>
</div>
<div class="form_index2">
<%#= f.label :"" %>
<%= f.text_field :page, :class=> "span2" %>ページ(※半角数字の入力必須)
</div>
<div class="form_index4">
<%#= f.label :"何行目あたり?" %>
<%= f.text_field :line, :class=> "span1" %>行目あたり(※半角数字。入力は任意)
</div>
<div class="form_index4">
<%= f.label :"投稿内容(※必須)" %>
<%= f.text_area :content ,:class => "span12",:size => "20x5" %>
</div>
<div class="actions">
<%= f.submit "投稿"%>
</div>
<% end %>
<% end %>
</div> <!--post_on_group-->
<div>
※この本に関する投稿をページ数や投稿内容から検索できます。
</div>
<div class= "form_index">
<%= form_tag({:action=>"show"}, {:method=>"get"}) do %>
<div class="from_field_index5">
<%= text_field_tag 'page' ,'', :class=> "span3" %>
<%= submit_tag 'ページ' %>
<% end %>
</div>
</div>
<div class= "form">
<%= form_tag({:action=>"show"}, {:method=>"get"}) do %>
<div class="from_field_index">
<%= text_field_tag 'content' %>
<%= submit_tag '投稿内容' %>
<% end %>
</div>
</div>
<hr>
<br/>
<% if #gms %>
<% #gms.each do |gm| %>
<div class="message_area">
<div class="each_message">
<%= image_tag gm.group.imageurl,:width => '20', :height => '25' %>
<%= 'Page:' + gm.page.to_s + '&' %><%= 'Line:' + gm.line.to_s %>
<%= gm.member.name %>
(<%= gm.created_at.strftime'%Y-%m-%d %H:%M' %>)
<div class="group_message">
<p class="message_content"><a href="/group_messages/<%= gm.id%>" > <%= truncate(gm.content, { :length => 300}) %></a></p>
</div><!--group_message-->
<br/>
<% if gm.group_message_comments.present? %>
<% gm.group_message_comments.order("created_at asc").each do |gmsc|%>
<div class="group_message_comment">
<p><%= gmsc.member ? gmsc.member.name : "unknown" %> (<%= gmsc.created_at.strftime'%Y-%m-%d %H:%M' %>)</p>
<%= truncate(gmsc.content, { :length => 300}) %>
</div> <!--group_message_comment-->
<br/>
<% end %><!-- each do -- >
<% else %>
<% end %> <!--if -->
</div><!--each_message-->
<br>
</div> <!--message_area-->
<% end %>
<% else %>
<% #group_messages.each do |gm| %>
<div class="message_area">
<div class="each_message">
<%= image_tag gm.group.imageurl,:width => '20', :height => '25' %>
<%= 'Page:' + gm.page.to_s + '&' %><%= 'Line:' + gm.line.to_s %>
<%= gm.member.name %>
(<%= gm.created_at.strftime'%Y-%m-%d %H:%M' %>)
<div class="group_message">
<p class="message_content"><a href="/group_messages/<%= gm.id%>" > <%= truncate(gm.content, { :length => 300}) %></a></p>
</div><!--group_message-->
<br/>
<% if gm.group_message_comments.present? %>
<% gm.group_message_comments.each do |gmsc|%>
<div class="group_message_comment">
<p><%= gmsc.member ? gmsc.member.name : "unknown" %> (<%= gmsc.created_at.strftime'%Y-%m-%d %H:%M' %>)</p>
<%= truncate(gmsc.content, {:length =>300}) %>
</div> <!--group_message_comment-->
<br/>
<% end %><!-- each do -- >
<% else %>
<% end %> <!--if -->
</div><!--each_message-->
<br>
</div> <!--message_area-->
<% end %> <!--each do -->
<% end %>
</div><!--span8-->
The way to check login and redirect, is by using the callback of before_filter
before_filter :check_login
check_login will redirect user to login action unless redirect
def check_login
unless session[:user_id]
flash[:error] = "You must be logged in to access this section"
redirect_to '/members/login'
end
end
If you want to enable access just for some actions - you can exclude them:
before_filter :check_login, :except => [:index, :show, etc..]

ArgumentError at /members/18 wrong number of arguments (3 for 2)

Now I am trying to set up ajax form_tag. And I wrote the codes below but got an error message around
<%= text_field_tag 'name' %>
☆error message
ArgumentError at /members/18
wrong number of arguments (3 for 2)
Could you tell me how to modify my codes??
☆show.html.erb(members)
<h4 class="title">ユーザーの投稿</h4>
※同じ本を読んでいる全ユーザーの投稿が表示されます。<br/>
<div class= "form_index">
<%= form_tag({:action=>"show"}, {:method =>"get"}, {:remote=>true}) do %>
<div class="from_field_index">
<%= text_field_tag 'name' %>
<%= submit_tag '本検索' %>
<% end %>
</div>
</div>
<div class= "form">
<%= form_tag({:action=>"show"}, {:method =>"get"}, {:remote=>true}) do %>
<div class="from_field_index">
<%= text_field_tag 'content' %>
<%= submit_tag '文検索' %>
<% end %>
</div>
</div>
<div id="show"></div>
☆members_controller
#gmsg_users = GroupMessage.where(:group_id => join_groups).order("created_at desc").limit(15)
#gmsg_users.scoped
if params[:name].present?
#gmsg_users = #gmsg_users.where("name like ?" , "%" + params[:name] + "%")
end
if params[:content].present?
#gmsg_users = #gmsg_users.where("name like ?" , "%" + params[:content] + "%")
end
☆show.js.erb
$('#show').html(
"<%= escape_javascript(render :partial => 'search_result')%>"
);
☆_search_result.html.erb
<div class="message_area">
<% #gmsg_users.each do |gmsg| %>
<div class="each_message">
<a href="/groups/<%= gmsg.group.id%>" ><%= image_tag gmsg.group.imageurl,:width => '20', :height => '25' %><%= "(" + gmsg.group.name + ")" %></a>
<%= 'Page:' + gmsg.page.to_s + '&' %><%= 'Line:' + gmsg.line.to_s %>
<%= gmsg.member.name %>
(<%= gmsg.created_at.strftime'%Y-%m-%d %H:%M' %>)
<div class="group_message">
<p class="message_content"><a href="/group_messages/<%= gmsg.id%>" > <%= truncate(gmsg.content, { :length => 50}) %></a></p>
</div><!--group_message-->
<br/>
<% if gmsg.group_message_comments.present? %>
<% gmsg.group_message_comments.each do |gmsc|%>
<div class="group_message_comment">
<p><%= gmsc.member ? gmsc.member.name : "unknown" %> (<%= gmsc.created_at.strftime'%Y-%m-%d %H:%M' %>)</p>
<%= gmsc.content %>
<br/>
</div> <!--group_message_comment-->
<% end %><!-- each do -- >
<% end %> <!--if -->
</div><!--each_message-->
<br/>
<% end %> <!--each do -->
</div> <!--message_area-->
form_tag method receives 0 to 2 arguments.
But you passed 3 arguments ({:action=>"show"}, {:method =>"get"}, {:remote=>true}).
:method and :remote must be in same hash object.
<%= form_tag({:action => "show"}, {:method => "get", :remote => true}) do %>
See: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag

form_for remote true is not working in ruby on rails

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">All Tasks(<%= current_user.tasks.where(:status => 'active').count %>)</li>
<li >Dreams(<%= current_user.tasks.where(:importance => 'Highly Important', :status => 'active').count %>)</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">
Others
<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??
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
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') %>");

Resources