rails4-autocomplete gem with active_admin and belongs_to relation - ruby-on-rails

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.

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.

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

How to display a list of users with simple_form?

When I do this: <%= f.association :user, :collection => User.where(:country_id => 1) %>
My dropdown is populated with lines like this:
#<User:0x0000010b98d170>
Instead, I would like to display an email, that is tied to the id of the users.
I have yet to find how to override the value / content defaults of simple_form when using associations.
Can anyone help?
thank you,
P.
Although the page on github (https://github.com/plataformatec/simple_form) didn't say, but I guess it's the same as the example f.input :age, :collection => 18..60
You could use :label_method and :value_method:
f.association :user, :collection => User.where(:country_id => 1), :label_method => :name, :value_method => :id
I did not use it before. Please tell me if it does not work.

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

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