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' %>
Related
I am getting starting with RoR and this is my first attempt to use the new remote_function. I have sort of a project management app so you have projects (streams) and they have multiple tasks. Inside the stream show view, I want to list out the tasks with a checkbox next to them. If a user clicks on the checkbox, I want to post to /tasks/3 sending done=true so that the record will be probably updated.
Here is code I came up with:
<%= check_box_tag "id", "id", task.done,
:onclick => remote_function(
:update => "task",
:url => { :action => :update },
:with => "'done=true'",
:complete => "alert('hi')" ) %>
It does print the checkbox, and it does check it automatically depending on the status of task.done. But when I fire the onclick and watch the logs, I see the following entry:
ActionController::RoutingError (No route matches "/streams/3"):
"/streams/3" doesn't sound right, I would really be expecting to see something like /tasks/id instead.
Can you help me figure out what I am doing wrong?
Here is the full code for the view.
<tr>
<td class="task">
<span class="tasktitle">
<%= check_box_tag "id", "id", task.done,
:onclick => remote_function(
:update => "task",
:url => { :action => :update },
:with => "'done=true'",
:complete => "alert('hi')" ) %>
<span class="<%= if (task.done) then "editable_field_complete" else "editable_field" end %>" id="task_title_<%= task.id %>">
<%= best_in_place task, :title, :type => :input %>
</span>
</span>
<span class="taskdelete"><%= link_to "delete", task, :method => :delete, :class => "delete",
:confirm => "You sure?",
:title => task.title %></span>
<span class="taskcreated">
Created <%= time_ago_in_words(task.created_at) %> ago.
</span>
</td>
</tr>
Thanks a lot guys
Error reason:
Forgot to mention controller pointer
Suggestoin:
http://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html
Always Refer api for rails its quite helpful for me.
Is the checkbox within a 'stream' view?
Simply write :url => { :action => :update, :controller => 'tasks'}. This should solve your problem.
i have used observed_field for arrays of data... but i am having trouble making a simple, one field application of it work. i must be missing a very simple detail. have searched extensively for basic syntax and cannot find anything that applies. when i change the selection, the change is not saved and there is no error msg.
<% form_for :team, :url => update_item_leader_group_path do |f| %>
<%= f.select :item_id, #selection_collection %>
<span id="trigger_spinner" style="visibility: hidden;">
<img src="/images/spinner.gif" alt="Loading..." />
</span>
<%= observe_field 'group_item_id',
:url => { :controller => :group, :action => :update_item },
:method => :put,
:with => "'trig=' + $('group_item_id').value" %>
:loading => "$('trigger_spinner').setStyle({visibility: 'visible'});",
:complete => "$('trigger_spinner').setStyle({visibility: 'hidden'});" %>
<% end %>
The onchange event may not be observed until you leave the focus of the select field. To watch some field changes instantly your only option is to use a form observer.
BTW: The dom id of your field is probably not item_id due to usage of the form builder. #Swards already pointed that out.
The id of the field is usually something like id='group_item_id', could this be the issue?
corrected syntax above
1. form path
2. observer "url"
3. observer "with"
there was also a routing problem. i had to move update_item from a map.resources member to collection
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...
I have a single select_tag with categories gathered from array in controller. When the user selects a category I want the application to redirect to the selected category. I have the following code in my view which. (I've tried both using :method => :get and :post, only change is in development.log)
<%=select_tag "cat_selected", options_for_select(#cats_for_mt)%><br>
<%=observe_field 'cat_selected',
:url => {:action => :viewflokkur},
:with => 'cat',
:method => :get %>
When I select one of the options the following gets logged to development.log.
Processing CategoriesController#viewflokkur (for 127.0.0.1 at 2010-06-12 12:33:26) [GET]
Parameters: {"cat"=>"Taugasjúkraþjálfun", "authenticity_token"=> "B2u5ULNr7IJ/ta0+hiAMBjmjEtTtc/yMAQQvSxFn2d0="}
Rendering template within layouts/main
Rendering categories/viewflokkur
Completed in 20ms (View: 18, DB: 0) | 200 OK [http://localhost/categories/viewflokkur?cat=Taugasj%C3%BAkra%C3%BEj%C3%A1lfun&authenticity_token=B2u5ULNr7IJ%2Fta0%2BhiAMBjmjEtTtc%2FyMAQQvSxFn2d0%3D]
According to this I should now be in "viewflokkur", but nothing changes in the browser window. Is there anything else I need to do, maybe in the controller?
BR,
Sindri
here is an example of Observe field :
<label for="search">Search term:</label>
<%= text_field_tag :search %>
<%= observe_field(:search,
:frequency => 0.5,
:update => :results,
:url => { :action => :search }) %>
<div id="results"></div>
Found here.
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" } %>