Display Array Data in Select Box - Ruby - ruby-on-rails

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"})}"} %>

Related

append hardcoded value in f.select_tag in rails 2

I have a dropdown in my rails partial which onchange calling a remote function...I am using rails2.3 verison.. In the drop down I am retreiving list of students like this..
<%= select :students, :id,
#students.map {|s| [s.name, s.id] },
{:prompt => "Select A Student"},
{:onchange => "#{remote_function(
:url => { :action => 'type' },
:method => 'get',
:with => "'stud_id='+value",
:before => "Element.show('loader')",
:success => "Element.hide('loader')" )}"} %>
This is working fine... I get list of students with their ids as values.. But I need to add one more option as "All students" to the dropdown so that I can view the details of all the students. How would I achieve it. Please put your thoughts on this as I am new to rails and learning it...
You can do something like this. Instead of using
#students.map {|s| [s.name, s.id] }
you can simply add a blank item to the array
[["All students",'']] + #students.map {|s| [s.name, s.id] }
I forget if Rails 2 allows you to do an { include_blank: true }, it is available in later versions.

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.

How to refresh the web page when user select a particular value on dropdown list on rails?

I'm developing project on rails 2.3.8 and I need to refresh the whole web page when user select particular selection on drop-down menu. How can I do it on rails ?
This is my drop down menu
<%= collection_select("country", "id", #countries , :id, :name, {:prompt => true}, :id => 'xx') %>
<%= observe_field('xx', :url => { :controller => 'calendar', :action => 'update_country' },:update => 'app_header',:with => "'con=' + escape(value)")%>
This finely load the countries so how I can reload the whole page ? please can some one explain me about this?
Simply add a html option to your collection select , onchange => "Javascript code to reload the page"
<%= collection_select("country", "id", #countries , :id, :name, {:prompt => true}, :id => 'xx', :onchange => "location.href = '#{root_url}'") %>
Or you can just refresh the current page:
<%= collection_select("country", "id", #countries , :id, :name, {:prompt => true}, :id => 'xx', :onchange => "location.href = window.location.href") %>

rails 2.1.2 collection_select with an :onchange and a :with

After a lot of searching and reading I've got a collection_select that looks like so
<%= collection_select :selection, :level, User::LEVELS, :to_s, :to_s,{:with => "this.value"},
{:onchange => remote_function(
:url => {:action => "updatelevel", :controller => "user", :id=> user.id})
} %>
However its not passing the selected value to my controller, the only thing I've ever got is a nil.
I've messed about with difference combinations of where :with should be and tried test strings but it never seems to do anything.
Am I missing something stupid? Is there a "definative" example I should be looking at?
Rails seems to change so fast that its hard to know which version a forum post is talking about and the api I read for collection_select doesn't show what I can put in the options hash.
I checked this out on my app running Rails 2.3.10. You have your 'with' parameter in the wrong spot, it is an option for the remote function, not for the collection select. Also, passing the value in that manner will get you a params hash that looks like {"134523456" => ""}, which probably isn't what you want. You have to have your 'with' value result in a string that is JavaScript centric.
<%= collection_select :selection, :level, User::LEVELS, :to_s, :to_s, {},
{:onchange => remote_function(
:url => {:action => "updatelevel", :controller => "user", :id=> user.id},
:with => "'level_id='+this.value"
)
}
%>

Ajax observe_field not working on 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...

Resources