I have an interesting routing situation that I'm having some trouble handling... This is for a rails 2.3 app, here's the scenario:
route a:
/:trans_type/:country_code/:property_types
route b:
/:trans_type/:country_code/:location
These two routes share the same location in the route so there needs to be a :requirement set on either :location or :property_types to differentiate the two.
Since :location is pretty wide open, :property_types is the way to go because I have a module that can easily compile a list of of all existing property types to do a regex match against.
The problem:
/for-sale/us/apartment-loft
Since :property_types is an array, I have to parse multiple property_types out of the params object (outside of the router, obviously). If I have a case like this, it's not possible for me to do a regex match in the router against a known property type because :property_types can potentially return several hyphenated types.
My question then is, Is it possible for me to take the :property_types string and modify it specifically for the :requirements matching? If I can replace 'apartment-loft' to just be 'apartment' for that match (/^\w+/), then I have something I can realistically regex match against.
Here's what the two routes actually look like:
map.location ":transaction/:country_code/:property_types",
:controller => "search",
:action => "location",
:requirements => { :transaction => /(for-sale|for-rent|auction|new_development)/, :country_code => /\w\w/, :property_types => /(#{prop_types})/ }
map.location ":transaction/:country_code/:location",
:controller => "search",
:action => "location",
:requirements => { :transaction => /(for-sale|for-rent|auction|new_development)/, :country_code => /\w\w/}
The above implementation works fine for routes that have one property_type, but for routes that have multiple hyphenated property_types, I'm hitting a wall.
If it's feasible, I would suggest creating explicit routes and controller actions for these two cases:
map.location ":transaction/:country_code/property-types/:property_types",
:controller => "search",
:action => "by_property_type",
:requirements => { :transaction => /(for-sale|for-rent|auction|new_development)/, :country_code => /\w\w/, :property_types => /(#{prop_types})/ }
map.location ":transaction/:country_code/:location",
:controller => "search",
:action => "by_location",
:requirements => { :transaction => /(for-sale|for-rent|auction|new_development)/, :country_code => /\w\w/}
Then you don't have to worry so much about regex matching at the router and you can do it in the controller instead.
Related
I was trying to accomplish the following:
<%= link_to "Log out", { :controller
=> 'users', :action => 'logout' }, :class => 'menulink2' %>
But it didn't work, it always redirected me to a show view. I had to had the following to my routes.rb:
map.connect 'users/logout',
:controller => 'users', :action =>
'logout'
Why didn't rails recognize the action I was passing ('logout') ?
That logic has to be specified somewhere. There's got to be some mapping from the hash {:controller => 'users', :action => 'logout'} to a url, and the place that's done in rails is the routes.rb file. In older versions of rails many routes.rb came with a default at the end:
map.connect ':controller(/:action/(:id(.:format)))'
Which would make it so that most any :controller, :action hash could be specified and then routed to host.url/:controller/:action.
With the more modern versions resource-based routes are heavily favored, and controllers which don't follow rails' REST conventions (i.e. having only :index,:show,:create,:new,:edit,:update,:destroy methods) generally have to have their routes explicitly specified in some way.
(Either with map.resources :users, :collection => {:get => :logout} or with map.connect( 'some_url', :controller => 'users', :action => 'logout'}))
I'm guessing, but the reason they did that is probably that the actions of a controller are really just its public methods.
It's frequently nice to have public methods in your controllers that aren't url-end-points for testing purposes.
For instance, you could have before_filters as public methods that you might want to test without having to use #controller.send(:your_before_filter_method) in your test code.
So they whitelist the resource actions, and make the others unreachable by default. Let me look through the rails changelog and see if I'm right.
does anyone know if there is a rails routes converter online? I was not able to find one. I am trying to convert this line:
map.add_payment_profile 'add_payment_profile/:id', :controller => 'payment_profile_controller', :action => 'add_payment_profile'
Thanks!! So if I understand correctly:
map.create_cim_payment_profile 'create_cim_payment_profile_user', :controller => 'authorize_net', :action => 'create_cim_payment_profile', :only => :post would be
match 'create_cim_payment_profile_user' => 'payment_profile#create_cim_payment_profile', :as => :create_cim_payment_profile
I believe what you're looking for is
match 'add_payment_profile/:id' => 'payment_profile#add_payment_profile',
:as => :add_payment_profile
Rails 3 has a new controller#action shorthand for mapping controller actions. You will also need to specify :as to created a named route.
may be this will work:
match 'add_payment_profile/:id',:controller => "payment_profile_controller",
:action => "add_payment_profile"
I have two different models that can show up in a category. In my routes.rb, I would like to have something like this:
ActionController::Routing::Routes.draw do |map|
map.top_list ':category/:foo', :controller => 'foo', :action => 'show'
map.top_list ':category/:bar', :controller => 'bar', :action => 'show'
end
This works fine when I load a URL like "/some-category-name/some-foo-name", where "some-foo-name" can be loaded by the FooController like so:
class FooController < ApplicationController
def show
#foo = Foo.find_by_url! params[:foo]
end
end
But when I try to request a Bar, like "/some-category-name/some-bar-name", I get a "ActiveRecord::RecordNotFound in FooController#show". I know that I can solve this problem by requiring that all Foo names start with "foo-" and all Bar names start with "bar-", then defining routes like this:
ActionController::Routing::Routes.draw do |map|
map.top_list ':category/:foo', :controller => 'foo', :action => 'show', :requirements => { :name => /^foo-/ }
map.top_list ':category/:bar', :controller => 'bar', :action => 'show', :requirements => { :name => /^bar-/ }
end
But forcing this restriction on names is quite suboptimal. I found the following, which looks like it might work for me: Different routes but using the same controller for model subclasses in Rails. However, I don't quite follow the example, so I don't know if this would solve my problem. It is also not great that my Foo and BarController would have to inherit from CategoryController.
One thought that occurred to me is that I could try to look up the Foo, then fall back to the BarController if that fails. I can easily do this in the FooController and redirect to the BarController, but this is not really OK, since all the requests for Bars will be logged as FooController#show in the Rails log. However, if I could somehow configure the routes to call a method to determine what to route to based on the basename of the URL, I could get the behaviour that I need; e.g.
ActionController::Routing::Routes.draw do |map|
is_a_foo = Proc.new {|name| Foo.find_by_url! name && true }
map.top_list ':category/:foo', :controller => 'foo', :action => 'show', :requirements => { :name => is_a_foo }
map.top_list ':category/:bar', :controller => 'bar', :action => 'show'
end
Is there any way to do this in bog-standard Rails 2?
I ended up writing a little Rails plugin that allows me to write a route like this:
map.with_options :category => /[-A-Za-z0-9]+/ do |m|
# Since both foos and bars can appear under category, we define a route for foo that only matches when
# the foo can be found in the database, then fall back to the bar route
m.foo ':category/:foo', :controller => 'foo', :action => 'show', :conditions => {
:url => { :find_by_url => Foo }
}
m.bar ':category/:bar', :controller => 'bar', :action => 'show'
end
Until I get permission to open source the code, I have to leave the implementation as an exercise for the reader, but here's how I got there:
Monkey-patching Rails: Extending Routes #2
Under the hood: route recognition in Rails
The Complete Guide to Rails Plugins: Part II
I'll update this answer with a github link when I can. :)
This seems wrong since you are using the same route to two controllers. Rails will choose one only as you noticed.
It would be nicer to use something like this:
map.top_list ':category/foo/:foo', :controller => 'foo', :action => 'show', :requirements => { :name => is_a_foo }
map.top_list ':category/bar/:bar', :controller => 'bar', :action => 'show'
Since in provided routes you give two params each time. It does not matter that you name them differently. It just matches both.
Is there a simpler way of writing this:
map.old_site_cusom_packages '/customs_packages_options.html', :controller => :public, :action => :redirect_to_home
map.old_corporate '/corporate.html', :controller => :public, :action => :redirect_to_home
map.old_track '/track.html', :controller => :public, :action => :redirect_to_home
map.old_links '/links.html', :controller => :public, :action => :redirect_to_home
map.old_contact '/contact.html', :controller => :public, :action => :redirect_to_home
I want to send many named routes to one action on one controller, I'm making sure url's left over from an old site redirect to the correct pages.
Cheers.
Use the with_options method:
map.with_options :controller => :public, :action => :redirect_to_home do |p|
p.old_site_custom_packages '/customs_packages_options.html'
p.old_corporate '/corporate.html'
p.old_track '/track.html'
p.old_links '/links.html'
p.old_contact '/contact.html'
end
You can always write a multi-purpose route with a regular expression to capture the details:
old_content_names_regexp = Regexp.new(%w[
customs_packages_options
corporate
track
links
contact
].join('|'))
map.old_content '/:page_name.html',
:controller => :public,
:action => :redirect_to_home,
:requirements => {
:page_name => old_content_names_regexp
}
That should capture specific pages and redirect them accordingly. A more robust solution is to have some kind of lookup table in a database that is checked before serving any content or 404-type pages.
Edit: For named routes, it's an easy alteration:
%w[
customs_packages_options
corporate
track
links
contact
].each do |old_path|
map.send(:"old_#{old_path}",
"/#{old_path}.html",
:controller => :public,
:action => :redirect_to_home,
)
end
In most cases the old routes can be rewritten using the singular legacy route listed first. It's also best to keep the routing table as trim as possible. The second method is more of a crutch to try and bridge the old routes.
I am looking to do something similar a wordpress slug where I have a URL like this while maintaining RESTful routing:
http://foo.com/blog/2009/12/04/article-title
The reason I am interested in keep RESTFUL routing is that I am unable to use many plugins because I am using custom routes.
I have already done the RESTful appearance with:
map.connect '/blog/:year/:mon/:day/:slug',
:controller => 'posts', :action => 'show',
:year => /\d{4}/, :month => /\d{2}/,
:day => /\d{2}/, :slug => /.+/,
:requirements => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/, :slug => /.+/ }
In order to write the links, I had to write custom link_to helpers to generate the proper URLs. I really would like to make this RESTful and have the link_to post_path( #post ) yield the URL above and the link_to edit_post_path(#post) ...article-title/edit
I also have :has_many => [:comments] and I would that to work as well. The link_to that I have tried looks like this:
'posts', :action => 'show', :year => recent_post.datetime.year.to_s,
:month => sprintf('%.2d', recent_post.datetime.mon.to_i),
:day => sprintf('%.2d', recent_post.datetime.mday.to_i),
:slug => recent_post.slug %>
and yields this (which isn't what I want):
http://foo.com/posts/show?day=30&month=11&slug=welcome-to-support-skydivers&year=2009
I'm not sure what I am doing wrong. Is it even possible to accomplish this?
I think it's not working because you're not using a custom route. I do this all of the time. I simply setup a simple custom route:
map.present_page '/blog/:year/:month/:day/:slug',
:controller => 'posts', :action => 'show'
Then you should be able to do:
present_page_path(:year => 2009,
:month => "December",
:day => "13",
:slug => "just-an-example")
The reason you're getting a query string is most likely because rails isn't making the connection to your route for whatever reason. Using a named route explicitly tells rails to use that route. Let me know if that solves it for you!
Here's how I went about this...
First, I'm not trying to use the route-generated url method. Also, I'm not going to the same extent as you in terms of checking formatting of the date parameters. Since I'm auto-generating the datestamps and the URL creation, I'm not concerned about format validity, I'm simply formatting a ActiveSupport::TimeWithZone object.
Let's start with the relevant route:
map.post_by_date 'content/:year/:month/:day/:slug',
:controller => 'posts',
:action => 'show_by_date_slug'
Since I didn't want to worry about argument formatting, or repetition, I created a helper method and included the helper in the relevant controller:
def pubdate_slug_url(post)
year = post.published_on.strftime('%Y')
month = post.published_on.strftime('%m')
day = post.published_on.strftime('%d')
url = "/" + ["content", year, month, day, post.slug].join("/")
return url
end
Finally, in my view, I simply call the method, passing in my Post object:
<h2><%= link_to post.headline, pubdate_slug_url(post) %></h2>
I end up with a url like: http://wallscorp.us/content/2009/12/06/links
Cheers.