Ajax observe_field not working on ruby on rails - ruby-on-rails

I am trying to create some kind of search in my ruby on rails application and I want to add ajax support. I want when a user types in a search box a word, ajax automatically generates the results. And I have searched almost all internet and I couldn't find answer to following question:
When I type into my text area nothing happens. It is like ajax is not working at all.
Tre problem is in observe_field which should observe my id= 'query' but it seem like it not doing anything
I am using Prototype 1.6.1 and the newest version of rails.
The code I wrote is:
viewer:
<form name="sform" action="" style="display:inline;">
<label for="item_name">Filter on Property No : </label>
<%= text_field_tag(:query, params['query'], :size => 10, :id => 'query' ) %>
</form>
<%= image_tag("/images/system/blue_small.gif",
:align => "absmiddle",
:border => 0,
:id => "spinner",
:style =>"display: none;" ) %>
</p>
<%= observe_field 'query', :frequency => 1,
:update => 'table',
:before => "$('#spinner').show()",
:success => "$('#spinner').hide()",
:url => {:action => 'list'},
:with => 'query' %>
<div id="table">
<%= render :partial => "items_list" %>
</div>
And my controler is:
def list
sort = case params['sort']
when "Title" then "title"
when "address" then "address"
when "close date" then "close_date"
when "property_no_reverse" then "property_no DESC"
when "address_reverse" then "address DESC"
when "close_date_reverse" then "close_date DESC"
end
conditions = ["title LIKE ?", "%#{params[:query]}%"] unless params[:query].nil?
#total = Message.count(:conditions => conditions)
#accounts = Message.paginate :page => params[:page], :per_page => 10, :order => sort, :conditions => conditions
if request.xml_http_request?
render :partial => "items_list", :layout => false
end
end
I would greatly appreciate any help I can get!

You said your using the newest version of Rails, if that is Rails 3.0.0 then observe_field and the way they are doing javascript is definitely different. Either downgrade to Rails 2.3.8 (I would not), learn the new way, or use the old way in Rails 3.0.0
New Way in Rails 3.0.0: http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/
Old way in Rails 3.0.0: http://github.com/rails/prototype_legacy_helper
If you want 2.3.8 I can show you observe field, just let me know...

Related

Display Array Data in Select Box - Ruby

I am a newbie to Ruby and I am facing an issue.
I am trying to display an array data in select box but somehow it shows empty box.
Here is my controller :-
def update_center
#center = ["center1", "center2"]
render :update do |page|
page.replace_html "center_list", :partial => "center_list"
end
end
Here is my view code :-
<%= select :abc, :center, #center,
{:prompt => "Select Center"},
{:onChange => "#{remote_function(:url => {:action => "update_b"}}"} %>
It renders correctly but in the select box it just prompts Select Center.
Kindly please let me know where am I going wrong. Code is in ruby 1.8.7
Thank you
I just tried, everything seems to be correct, except your syntax above should be
<%= select :abc, :center, #center,
{:prompt => "Select Center"},
{:onChange => "#{remote_function(:url => {:action => "update_b"})}"} %>

how to paginate combined with anchor id

I'm very new to Ruby on Rails and learning while trying to fix some bugs on my company website. I'm trying to paginate a collection of records combined with a particular anchor, i.e. when user clicks on the next/previous page, the pagination happens and user lands on a particular section of the page. This is how my code looks at the moment:
view
<%= page_navigation_links #student_logs, :page %></p>
controller:
#student_logs.paginate(:page => params[:page], :per_page => 10)
application_helper
def page_navigation_links(pages, param_name=:page)
will_paginate(pages, :class => 'pagination', :inner_window => 2, :outer_window => 0, :renderer => BootstrapHelper::LinkRenderer, :previous_label => '←'.html_safe, :next_label => '→'.html_safe, :param_name => param_name)
end
This works fine for my pagination, but I need to fit a reference to an 'anchor id' into this code, like Student Logs, so that when user navigates to a different page the browser navigates the user to the heading with id: "student_logs".
I can't figure out how to do this. Can anyone help?
I do not have direct experience with it. However, this link shows a way. I could not test but you can certainly do.
<%= will_paginate #posts, :params => {:anchor => i} %>
If this works, then you can utilize this way in your helper:
def page_navigation_links(pages, param_name=:page)
will_paginate(pages, :params => {:anchor => "#ANCHOR"}, :class => 'pagination', :inner_window => 2, :outer_window => 0, :renderer => BootstrapHelper::LinkRenderer, :previous_label => '←'.html_safe, :next_label => '→'.html_safe, :param_name => param_name)
end
You can replace #ANCHOR as you need.

Rails will_paginage

i am using will_paginate for pagination,
When i reach the last page and then click on Next link its again taking me to first page,
Actually i want to disable the Next button when i reach the last page.
please advise me how to solve this..
this is the code i used for pagination
<%= will_paginate #users,
:prev_label => "Preevious",
:next_label => "Next",
:page_links =>true,
:renderer => PaginationListLinkRenderer
%>
Controller:
#users = User.paginate :per_page => 5,
:page => params[:page],
:order => 'name ASC',
:conditions => ""
View:
<%= will_paginate #users,
:previous_label => "Previous",
:next_label => "Next",
:inner_window => "2",
:outer_window => "0" %>
Above is all my code and it works for me in will_paginate. Once reached down the last page, the "Next" link change to bolded text, so as the last page number (see image below).
I am using
<%= will_paginate #users %>
and it is working fine. It disables the next link when i reach the last page.

Observe field is not doing anything

I want a search field to start outputting results once a user starts typing.
This is my view:
<%= observe_field 'keyword',
:url => { :action=> 'search_results' },
:frequency => 0.5,
:update => 'results',
:loading => "$('.spinner').show()",
:complete => visual_effect(:slide_down, "results", :duration => 0.9),
:with => 'keyword' %>
<%= form_remote_tag :url =>{ :action => :search_results },
:update => "results",
:loading => "$('.spinner').show()",
:complete => visual_effect(:slide_down, "results", :duration => 0.9) %>
<fieldset>
<legend>Tutor Search.</legend>
<label for="searchItField">Keyword Search.</label>
<%= text_field_tag(:keyword) %>
<span id="Submit_search">
<span id="Submit_search_hover"><%= submit_tag "Search", :name => nil %></span>
</span>
</fieldset>
</form>
<div id="results">
<img class="spinner" id="ajax_spinner" src="../images/ajax-loader.gif" alt="Comment being processed" style="display: none" />
</div>
In my controller is this:
def search_results
keyword = params[:keyword]
#tutors = Tutors.find(:all,:conditions => ["category LIKE ?", '%' + keyword + '%'])
end
Now if you go to http://avandata.net/tutor/tutors/search and type math in the search field and click submit it works. BUT the observe field does not do anything when typing. I have been trying to figure this out forever.
Here is what my development log looks like.
Here is my log/development Processing TutorsController#search (for 98.254.132.228 at 2010-07-30 09:29:52) [GET] Rendering tutors/search Completed in 5ms (View: 3, DB: 0) | 200 OK [avandata.net/tutor/tutors/search]
And for the submit search button it looks like this in the log:
Processing TutorsController#search_results (for 98.254.132.228 at 2010-07-30 13:28:22) [POST]
Parameters: {"authenticity_token"=>"KNsIfr69fDTVGEZnwQoyXhnnuK5ZP6QIwq/zHRVYWqQ=", "keyword"=>"math"}
[4;36;1mTutors Load (0.1ms)[0m [0;1mSELECT * FROM `tutors` WHERE (category LIKE '%math%') [0m
Rendering tutors/search_results
[4;35;1mTutors Columns (3.4ms)[0m [0mSHOW FIELDS FROM `tutors`[0m
Completed in 8ms (View: 2, DB: 4) | 200 OK [http://avandata.net/tutor/tutors/search_results]
If you look at that page under Firebug you will see a Javascript error occur as soon as you hover over the text field. So that's why it's not working for you. As to what's causing the error is unclear.
I did note that the JQuery library that the page is loading is quite old (1.2.6). I would recommend updating your supporting JQuery libraries to at least the versions contained in the JQuery repository here
If that doesn't fix your problem then I'd suggest reconsidering your use of JRails altogether. I've always thought that it was somewhat of a pointless project - cloning the RJS interfaces but built on JQuery instead of Prototype. My opinion notwithstanding it doesn't seem to have a lot of traction in the community so there's the concern about future support.
If you like RJS then just use the defaults that Rails comes with. They work well if you like that sort of thing. If you like JQuery then use it directly. It's a great library and very intuitive to use. In the time you spent looking at this not working you could have coded an equivalent functional version in JQuery several times over.
I figured out that the problem was that the observe field had to go after the text field. Thats all it was. See below.
<%= form_remote_tag :url =>{ :action => :search_results },
:update => "results",
:loading => "$('.spinner').show()",
:complete => visual_effect(:slide_down, "results", :duration => 0.9) %>
<fieldset>
<legend>Tutor Search.</legend>
<label for="searchItField">Keyword Search.</label>
<%= text_field_tag(:keyword) %>
<span id="Submit_search">
<span id="Submit_search_hover"><%= submit_tag "Search", :name => nil %></span>
</span>
</fieldset>
</form>
<%= observe_field 'keyword',
:url => { :action=> 'search_results' },
:frequency => 0.5,
:update => 'results',
:loading => "$('.spinner').show()",
:complete => visual_effect(:slide_down, "results", :duration => 0.9),
:with => 'keyword' %>

How to update two f.select from 2 different tables on ruby on rails

I just started working on a Ruby On Rails project and i got to the point where i need to see the contacts of a company.
They should appear once the company is selected..
<p>
<%= f.label :empresa_id %><br />
<%= f.select(:empresa_id, #empresa.map {|e| [e.nombre, e.id]} )%>
</p>
<%= observe_field :empresa_id, :url=>{:action => "get_contactos",
:controller=> :contactos, :updatewith =>:empresa_id} %>
But nothing happens, i don't see even a error on the script/server.
Can someone point me in the right direction?
http://nealenssle.com/blog/2007/04/12/how-to-dynamically-update-form-elements-in-rails-using-ajax/
I see on the link that a guy did the exact same thing i need but did not post any info.
Cheers.
Kinda got this working.. here's my update..
This is the form were my select boxes are placed:
">
"contacto_id",
:with => "empresa_id", :url => { :controller => "contactos",
:action => "get_contactos" } %>
The function get_contactos on the contactos controller :
def get_contactos
#contacto = Contacto.find_all_by_empresa_id(params[:empresa_id])
render :layout => false
end
And the view get_contactos.rhtml (contactos):
">%>
I just need to bind the form to the correct fields and the job will be finished.
I couldn't get this to work by observing the field :empresa_id but "empresa_id" works..
Edit: Got it to work.
I just binded the field on the form to the proper ones on the model and now i have the data =)
">
"llamada_contacto_id",
:with => "empresa_id", :url => { :controller => "contactos",
:action => "get_contactos" } %>

Resources