Specifying a param from a route - ruby-on-rails

Consider a PersonController which has a list action. A user can choose to list all people, or only males or females; currently, to do that, they'd have to go to /people/m or /people/f, corresponding to the route
map.list_people "people/:type",
:conditions => { :method => :get },
:requirements => { :type => /a|m|f/ },
:defaults => { :type => 'a' }
(/people/a works the same as just /people/, and lists all people).
I want to change my routing so that I could have two routes, /males/ and /females/ (instead of people/:type), both of which would go to PersonController#list (DRY -- aside from an extra parameter to what's being searched, everything else is identical), but will inherently set the type -- is there a way to do this?

map.with_options(:controller => "people", :action => "index") do |people|
people.males 'males', :type => "m"
people.females 'females', :type => "f"
end
Then you should be able to do males_path or males_url to get the path for this, and I'm sure you can guess what you do to get the path to females.

Related

Rails passing a list of objects to another controller with link_to_remote

Is it possible to pass a list of objects to another controller in Ruby on Rails?
I have experimented with the following example:
class Student
...
end
student_list = [std1, std2, ... stdn]
When I use
<%= link_to_remote("example", :url => {:controller => 'class/assignment',
:action => 'homework',
:student => student_list})%>
It did not work the way I expected. params[:student] is equal to "student" (string literal).
Is there anything I did wrong, or an alternate way of doing it?

Rails: Setting class and data-tag of an HTML attribute with a single rails method

I'm currently working on a tour interface that guides new users around my site. I have a Tour model that has many TourStops, each of which contains information about a section of the site.
Basically, I'd like to write a function for the Tour model that -- when passed the number of a TourStop -- generates the correct class and data attribute for the HTML element it's attatched to. For example, I'd like
<%= link_to image_tag("new_button.png", tour.stop_data(1), :title => 'Add new asset'), new_asset_path %>
to call a function and return something like
def stop_data(order)
" :class => '#{tour_stops.find_by_order(order).name}',
:data => '{:order => order}'"
end
creating a link_to tag like:
<%= link_to image_tag("new_button.png", :class => 'tour_stop_1',
:data => {:order => 1}, :title => 'Add new asset'), new_asset_path %>
The above code doesn't work. Is something like this even possible? If not, what's a better approach I might take?
The image_tag accepts two parameters. A source, and a options Hash.
What you are trying to do is squeezing your return value from stop_data into this options Hash.
In order to get this to work, you first, need to return a Hash from stop_data, and second, make sure you pass only two arguments to image_tag - the source, and the options.
First:
def stop_data(order)
{
:class => tour_stops.find_by_order(order).name,
:data => { :order => order } # you may need order.to_json
}
end
Second:
link_to image_tag("new_button.png", tour.stop_data(1), :title => "Add new asset"), new_asset_path
This looks like it will work, but it won't, since your'e passing three parameters to image_tag.
When you do the following:
image_tag("new_button.png", :class => "tour_stop_1", :data => { :order => 1 }, :title => "Add new asset")
It looks like you're passing even 4 parameters to image_tag, but in fact they are only two. In Ruby, when the last parameter of a method is a Hash, you don't need to wrap the Hash key/value pairs in curly braces ({}), so the example above is essentially the same as
image_tag("new_button.png", { :class => "tour_stop_1", :data => { :order => 1 }, :title => "Add new asset" })
Now, to get your helper to work with image_tag, you need to merge the options, so they become only one Hash.
link_to image_tag("new_button.png", tour.stop_data(1).merge(:title => "Add new asset")), new_asset_path
Again, we're omitting the curly braces when calling merge, because it's only (and therefore last) parameter is a Hash. The outcome is the same as:
tour.stop_data(1).merge({ :title => "Add new asset" })

What is the ActiveScaffold syntax for a 'has_many' relation link in list view if placed in the helper?

A 'product' has many 'parallel_products':
class Product < ActiveRecord::Base
has_many :parallel_products, :class_name => "Product", :foreign_key => "master_product_id"
end
In the controller I add the 'parallel_products' column to the list view:
class ProductsController < ApplicationController
active_scaffold :product do |config|
config.list.columns = [ :parallel_products ]
end
end
This gives me a ActiveScaffold generated link in the list view to view, create and edit parallel products of the selected product.
So far so good.
Now, I want to specify this 'parallel_products' link in the helper instead. No changes to the link itself, it should be exactly as the ActiveScaffold generated link. The reason is that I need to add a condition, so that the link is only shown under certain circumstances.
The ActiveScaffold generated link looks like this in the log:
Started GET "/products?assoc_id=6&association=parallel_products&eid=products_6_parallel_products&parent_scaffold=products&adapter=_list_inline_adapter" for 172.16.99.11 at 2012-03-05 09:37:45 +0100
Processing by ProductsController#index as JS
Parameters: {"assoc_id"=>"6", "association"=>"parallel_products", "eid"=>"products_6_parallel_products", "parent_scaffold"=>"products", "adapter"=>"_list_inline_adapter"}
My best proposal for the ActiveScaffold has_many relation link in the helper is:
link_to("link text", :controller => "products", :assoc_id => record.id, :association => "parallel_products", :eid => "products_#{record.id}_parallel_products", :parent_scaffold => "products", :adapter => "_list_inline_adapter")
This gives me in the log:
Started GET "/products?adapter=_list_inline_adapter&assoc_id=6&association=parallel_products&eid=products_6_parallel_products&parent_scaffold=products" for 172.16.99.11 at 2012-03-05 09:39:38 +0100
Processing by ProductsController#index as HTML
Parameters: {"adapter"=>"_list_inline_adapter", "assoc_id"=>"6", "association"=>"parallel_products", "eid"=>"products_6_parallel_products", "parent_scaffold"=>"products"}
My link does not work, but it seems to be very close. Only difference is that the generated link state 'ProductsController#index as JS' and my syntax state 'ProductsController#index as HTML'.
What is the correct ActiveScaffold syntax for making a 'has_many' relation list view link in the helper?
Thanks to Sergio Cambra for helping to solve this.
This is the syntax for a 'has_many' association link if placed in the helper:
link_to("link text", {:controller => "products", :association => "parallel_products",
:parent_scaffold => "products", :product_id => record.id}, :class => 'index as_action',
:id => "as_products-index-parallel_products-#{record.id}-link",
:remote => true, :data => {:position => :after, :action => 'index'})
To answer the question in full, this is how it can be implemented to exactly replace the autogenerated AS association link:
def parallel_products_column(record)
if product.parallel_products.blank?
link_text = "-"
css_class = "index as_action empty"
else
link_text = product.parallel_products[0...3].map{ |p| p.name }.join(", ")
if product.parallel_products.length > 3
link_text += ", … (#{product.parallel_products.length})"
end
css_class = "index as_action"
end
link_to(link_text.html_safe, {:controller => "products", :association => "parallel_products",
:parent_scaffold => "products", :product_id => record.id}, :class => css_class,
:id => "as_products-index-parallel_products-#{record.id}-link",
:remote => true, :data => {:position => :after, :action => 'index'})
end
You will need a small 'hack' for the css 'empty' class, example:
.active-scaffold a.empty {
display: block;
text-align: center;
}
Note:
This is for Rails/AS 3.1 or newer.

Search by ID, no keyword. Tried using :conditions but no result ouput

Using Thinking Sphinx, Rails 2.3.8.
I don't have a keyword to search, but I wanna search by shop_id which is indexed. Here's my code:
#country = Country.search '', {
:with => {:shop_id => params[:shop_id]},
:group_by => 'state_id',
:group_function => :attr,
:page => params[:page]
}
The one above works. But I thought the '' is rather redundant. So I replaced it with :conditions resulting as:
#country = Country.search :conditions => {
:with => {:shop_id => params[:shop_id]},
:group_by => 'state_id',
:group_function => :attr,
:page => params[:page]
}
But then it gives 0 result. What is the correct code?
Thanks.
Have a go in the console and see if you cant shift the :conditions around. It looks like by removing the blank string you simply have set a default param for the gem. Get the Gem in your Vendor and have a look at the Country Class to see what is accepts.

haml and no javascript? (rails: form_remote_tag and :with parameter)

i am trying to call a remote method to update page content via ajax/js.
either i am too tired already or haml is not parsing the following code correctly to send the value of the query field via prototype. any ideas?
- form_remote_tag(:url => {:controller => "search", :action => "line"},:with => "'query=' + $('query').value" ) do
%input{:type => 'text', :id => 'query'}
%input{:type => 'submit', :value => 'Search'}
thanks a lot!
t
Have you tried a
= form_remote_tag
instead of
- form_remote_tag
I'm new to HAML myself but I was under the impression that you'll need the form tag to be actually generated not just executed...
Try passing the :with as part of the options hash.
- form_remote_tag({ :url => {:controller => "search", :action => "line"}, :with => "'query=' + $('query').value" }) do
If that doesn't work, debug the problem: Look at the generated html. Is the text field with id query the only element in the page with that id? Is the js code correct? Use the Firebug console to ensure $('query').value returns whatever you've entered into the text field.
Still stuck? Add your generated html into your question so we can better help.
EDIT: Your query input tag does not have a name attribute. Without a name, the javascript helper code skips that field when serializing the form fields...also, you do not need the :with code.
%input{:type => 'text', :id => 'query', :name => 'query'}

Resources