Routing alias, is this possible?

I have a car model:

Routes

map.resources :vehicles, :has_many => :suppliers

Everything works fine, but the vehicle has the is_truck boolean attribute . I want to create an alias so that I can get the same resources by filtering only trucks, I tried:

Routes

map.trucks '/trucks', :controller => :vehicles, :action => :index, :is_truck => true
map.trucks '/trucks/by_supplier/:supplier', :controller => :vehicles, :action => :index, :is_truck => true

The first one works well, but when I search on the Form, the second one does not work and searches for all suppliers.

Controller:

class VehiclesController
   def index
     if params[:supplier]
       @vehicles = Vehicle.all :conditions => { :is_truck => params[:is_truck] }
     else
       @vehicles = Vehicle.all
     end
   end
   ...
end

Search Form:

<% form_for :truck, :url => {:controller => :trucks, :action => :index}, :html => {:method => :get} do |f| %>
  <% f.text_field :search %>
  <% f.submit 'Search Trucks' %>
<% end %>

Is map.resources possible as an alias?

+5
source share
5 answers

I found a cleaner way to do this, but the Search is still broken under a specific provider:

# Show all vehicles
map.connect '/vehicles/supplier/:supplier', :controller => :vehicles, :action => :index
map.resources :vehicles

# Only show trucks
map.connect '/trucks/supplier/:supplier', :controller => :vehicles, :action => :index, :is_truck => true
map.resources :vehicles, :as => 'trucks', :requirements => { :is_truck => true }

: http://api.rubyonrails.org/classes/ActionController/Resources.html

+1

:

map.resources :vehicles, :has_many => :suppliers,
                         :collection => { :trucks => :get }

rake routes . , :

trucks_vehicles GET /vehicles/trucks(.:format)
                    {:controller=>"vehicles", :action=>"trucks"}

, "", "". ( ), . , , , .

STI ( : ). - , :

class TrucksController < VehiclesController
  def new
    @is_truck = true
    super
  end
  ...
end

class TrucksController < VehiclesController
  before_filter :this_is_a_truck
  ...

  private

  def this_is_a_truck
    @is_truck = true
    super
  end
end

: ( is_truck):

class TrucksController < VehiclesController
  around_filter :with_truck_scope
  ...

  private

  # Scope every active record access with an is_truck condition
  # you may want to put this directly into the model to get rid of the .send
  # method and directly access "Vehicle.with_truck_scope &block" here
  def with_truck_scope(&block)
    Vehicle.send :with_scope, :find => { :conditions => "is_truck = 1" },
                              :create => { :is_truck => 1 }, &block
  end
end

:collection :member Rails.

+1

?

smth like:

class VehiclesController
   def index
     if params[:supplier]
       @vehicles = Vehicle.all :conditions => { :supplier_id => params[:supplier] }
     else
       @vehicles = Vehicle.all
     end
   end
end
0

, , :

map.trucks '/trucks/by_supplier/:supplier', :controller => :vehicles, :action => :index, :is_truck => true
map.trucks '/trucks', :controller => :vehicles, :action => :index, :is_truck => true

" - ", , map.trucks '/trucks' catch '/trucks/by_supplier/: . :

map.with_options :controller => :vehicles, :action => :index, :is_truck => true do |v|
  v.trucks '/trucks/by_supplier/:supplier'
  v.trucks '/trucks'
end
0
source

I recommend you use another resource by simply adding:

map.resources :vehicles, :as => :trucks, :has_many => :suppliers

Then process it in the controller with something like:

def index 
  conds = {}
  conds = { ... } if request.uri =~ /trucks/ # you can be more specific about the regexp if you need to
  @vehicles = Vehicle.all :conditions => conds      
end

What do you think about this?

0
source

All Articles