rails 2.1.2 collection_select with an :onchange and a :with - ruby-on-rails

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

Related

rails4-autocomplete gem with active_admin and belongs_to relation

Could anyone tell me the correct way for this ?
I've got situation like this one:
collection_action :autocomplete_order_zip_id, :method => :get
controller do
autocomplete :order, :zip_id do |item|
render json: Zip.all
end
And filter:
filter :zip_id_contains, :as => :autocomplete, :url => '/admin/orders/autocomplete_order_zip_id',
:label => "Search Email", :required => false,
:wrapper_html => { :style => "list-style: none" }
How can I pass my own searching logic for autocomplete ?
So I want customer to type ZIP name and system should find suggestions by zip_id in my order table.
I couldn't find the solution this way, so I've used the jquery select2 for input type="text" with ajax loading of information and my custom logic to get it.

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 can I make this RESTful when it's requested with few parameters at once?

I made my routing just like this. But what if genre was empty?
Isn't it going to redirect to example.com/shops/newest_first//california?
How can I solve this kind of routing and parameters problem??
routes.rb
match 'shops/:sort/:genre/:area', :to => 'shops#index'
view
<%= form_tag shops_path, :method => :get do %>
<%= select_tag :sort, options_from_collection_for_select(Sort.all, 'id', 'name', params[:sort]), :prompt => "Newest first" %>
<%= select_tag :genre, options_from_collection_for_select(Genre.all, 'id', 'name', params[:genre]), :prompt => "all" %>
<%= select_tag :area, options_from_collection_for_select(Area.all, 'id', 'name', params[:area]), :prompt => "all" %>
<% end %>
Another View
I would consider using GET params for things like area and sort since you are filtering indexes of other resources. You might also check the the section on Dynamic Segments in the guides, although that won’t help with the empty segment in the middle.

Rails route for button

I'm a rails newbie and a question on routes is confusing me.
On one of my pages, I have a form. In that form I allow the user to fill in some needed information and press a "submit" button.
I get:
No route matches {:action=>"inventory_test", :controller=>"test_types"}
I do have an action in the test_type controller for "inventory_test".
My confusion is that routes seem to be defined according to the REST model, such as /Users/edit/1. That's fine, but how does one create routes for things like buttons?
I may be naive, but it seems like if I tried to setup a route in the form:
match 'some/url' => 'controller#action'
then I'm essentially defining the action for the entire page. How do I define actions for elements on the page?
When this button is clicked, I want the action in the controller called. I'm looking for:
match "submit_button" => 'test_types#inventory_test'
I realize I'm likely misunderstanding the paradigm, so any education is greatly appreciated.
Code:
(Note that I haven't tested the form code yet, but hopefully you get the idea)
index.html.haml
%div
%table
%caption
Inventory Tests
%form
Inventory Run: %input {:type => 'text', :name=>'inventory_run'}
Inventory Class: %input {:type => 'text', :name=>'inventory_class'}
=button_to "Run Inventory Test", :action => 'inventory_test';
If you are submitting a normal form then this should help
The route match '/url' => "test_types#inventory_test" should work fine.
<%= form_for(#user, :url => "/url") do |f| %>
"Put your form code here"
<%= f.submit "Submit" %>
<% end %>
Revert back if any queries .
Edited as per code posted
%form{ :action => "inventory_test", :method => "post"}
%label{:for => "inventory_run"} Inventory Run:
%input{:type => "text", :name => "inventory_run"}
%label{:for => "inventory_class"} Inventory Class:
%input{:type => "text", :name => "inventory_class"}
%input{:type => "submit", :value => "Submit"}
Just check, it should work for you but i have not tried with it

Preseve line breaks from textarea in Rails controller submitted with submit_to_remote

I can't believe there isn't a standard way of doing this, but I'm submitting content from a textarea to a Rails controller, and it doesn't seem to preserve the line breaks (of any form).
Here is my view code:
f.text_area :keywords, :cols => 50, :rows => 10
submit_to_remote 'button', "#{t "add_keywords"}",
:html => {:id => 'add_keywords_button'},
:url => { :controller=> :keywords, :action => :add_to_site },
:escape => true,
:loading=>"Form.Element.disable('add_keyword_button')",
:complete=>"Form.Element.enable('add_keyword_button');",
:update => { :success => "keywords_table_decorator", :failure => "message"
After submitting this goes to a controller that just needs to be able to parse out each keyword, line by line. I've tried all of the variations on the following theme:
#keywords = params[:site_keywords][:keywords]
puts #keywords.gsub(/\n|\r|\r\n/,'*')
just to see if I can get something that I can do a further split with.
I'd appreciate advice on getting this to work.
Figured it out. I had this in my reset.css for all textareas:
white-space: normal;
Removing it made the problem go away.

Resources