We are in the process of taking a few pages out of our rails app to be served separately (they are a few static pages with some content being managed through a cms). The urls will stay the same. Our own routing system in front of the servers will decide which request should go to the rails app and which to the static part.
My question is about path helpers that we use quite a bit throughout the rails app, such as link_to about_path that generate mahwebsite.com/about. As I understand I can just leave them be, they will still generate correct urls. My only concern is that for them to work I'll have to keep the routings in the routes file, which will have to be connected to the dummy controller methods. Seems like a lot of redundant code just to fool rails into creating path helpers.
Alternatively, I can hard-code links to the static pages. But before I start replacing a whole lot of code, I'd like to know if there is a clean Railsy way to keep the path helpers without having to route to the redundant controllers.
Thanks.
Why not just create your own helper method? E.G.
# application_controller.rb
def about_path
"mahwebsite.com/about"
end
helper_method :about_path
alias_method :about_url, :about_path
This will overwrite any Rails helper method and do exactly what you're after :)
Hope this helps - give me a shout if you've any questions or comments.
How about
resources :custom_pages, only: [:your_options] do
get :view/:page_id_or_whatever_for_identify
end
and do the following content with the controller?
Related
I have a model called resource in my rails app and in need of modifying the return value of the helper *resource_path*, I've read some docs and SO Q/A and they're generally suggesting put the customized helper in *app/helpers/application_helper.rb*. The thing bothers me is that what do I do with the old auto generated helper? should I do something like
undef resource_path
before I go ahead and write my own helper? Currently I have a *resource_path* method defined within ApplicationHelper, interestingly when I open rails console, app.resource_path and helper.resource_path giving me different result.
Also, I'd like to hear a deeper explanation on how *_path* helpers implemented and how they are related to *link_to* helper, as the source code are kinda hard to read with so many meta programming techniques involved
Yes, you are able to do it like following:
resources :photos, as: 'images'
in your config/routes.rb file.
More details you can find here http://guides.rubyonrails.org/routing.html especially 4.3 Overriding the Named Helpers
So we use the same controllers to serve both mobile and desktop views of our site. We also use action caching heavily to cache the html for a page in memcache. I've been trying to figure out a way to globally change the caching prefix for all mobile requests to "views-mobile/" instead of the standard "views/". That way the mobile and and desktop pages will be saved under a different namespace so there are no conflicts in memcache.
We could do this per caches_action method by creating a custom cache_path using the controller variable for is_mobile?, but we'd prefer to do it globally somehow. Any suggestions? I imagine this would require monkey-patching ActionController::Caching but I can't figure out where it generates the "views/" prefix.
I'm sorry, I was Rails nubie, so I don't really understand about your question, but if it right, is this what you mean?
This is on my routes.rb:
scope "/administrator" do
resources :users
end
I changed my users_path 'prefix' to administrator. Sorry if wrong :D
I actually ended up figuring this out myself. Basically ActionController::Base uses a function called fragment_cache_key to generate the cache key for a specific fragment (which is what ActionCaching uses deep down). So you basically override that method and include your own logic for how to generate the prefix. This is how my method override looks:
# Monkey patch fragment_cache_key
def fragment_cache_key(key)
ActiveSupport::Cache.expand_cache_key(key.is_a?(Hash) ? url_for(key).split("://").last : key, mobile_device? ? "views-mobile" : "views")
end
Where mobile_device? is my own function that figures out whether the user is requesting the mobile or desktop version of the site.
My app consists of two rails servers with mostly different concerns sitting behind a reverse proxy. Let's call them Server1 and Server2. Occasionally, Server1 needs to render a link to a url on Server2. Is there a good way to use Rails route helpers for this? Specifically in Rails 2? I came up with this:
ActionController::Routing::Routes.draw do |map|
# other routes omitted
map.with_options(:host => 'server2.example.com') do |add|
# create a named route for 'http://server2.example.com/thingies'
add.server2_thingies '/thingies', :controller => 'fake'
# create a named route for 'http://server2.example.com/thingies/combobulate'
add.enhance_server2_thingies '/thingies/combobulate', :controller => 'fake'
# create a named route for 'http://server2.example.com/mabobs/combobulate'
add.enhance_server2_mabobs '/mabobs/combobulate', :controller => 'fake'
# etc..
end
end
So then I can use server2_thingies_url and such in my views. This works, but it makes me feel like a bad person because there is no FakeController and I certainly have no intention of routing requests to it. I considered making helper methods and placing them in app/controllers/application_controller.rb, but a colleague made the argument that it is best to keep all route helpers in routes.rb, so things like rake routes will be accurate, for instance. Is there a better way?
I think I'd make a counter-argument to your colleague: if you're having to dirty up routes.rb with a "FakeController", then your rake routes is still not going to be accurate. In fact, I'd say that this is exactly the kind of thing that a helper was meant to help: it's taking logic that belongs purely in the view (link generation), and removing it from your view templates. Helpers are also easier to maintain and tweak.
Another advantage to the helper style is that if and when it's time to upgrade to Rails 3.x, the less hackish your routes.rb file is, the happier you will be - and that I can attest to from experience. :)
I'd just pass in :host to your foo_path or foo_url calls.
Seems cleaner than messing around with a FakeController
Is there a method like "full_url" such that #comment.full_url or full_url_for(#comment) returns "http://www.host.com/comments/id" where www.host.com is the default host domain and id is #comment.id. Or, if not, what would be an elegant way to generate this url string?
I'm pretty new at Rails, most of the methods I've learned insert the tag and other markup.
url_for is not helping because I can't do something like the following:
url_for(#comment, {:only_path => false})
I've spent way too much time trying to figure this out. It came down to either hacking or asking for the right way on SO. Here I am.
If you are setting up your routes correctly in your config/routes.rb file then you should have access to named routes in your controller and in your views. Which should mean that all you should need to do is:
comment_path(#comment)
Or for the full url
comment_url(#comment)
To see a list of all of the routes from the command line, you can type rake routes from the project root. Welcome to rails! Here is a good resource for rails 3 routing: http://guides.rubyonrails.org/routing.html
some additional resources via Railscasts:
http://railscasts.com/episodes/231-routing-walkthrough
http://railscasts.com/episodes/232-routing-walkthrough-part-2
I need to use one of the resourceful controllers plugins - resources_controller/ resource_controller/make_resourceful as I have several polymorphic models and the models have to be either initialized/build depending on the route.
For example:
www.example.com/groups/1/pages
www.example.com/projects/1/pages
where page acts as polymorphic object as both Group and Project have many pages. So I am thinking of using one of the aforementioned plugins to make pages_controller adapt to both routes. All three plugins works fine and differences are just their implementation of recognizing the routes and loading the models based on that.
Now I want to add sub-domain support using Subdomain_fu plugin so the above example would be:
Site1.example.com/groups/1/pages
Site1.example.com/projects/1/pages
Site2.example.com/groups/2/pages
Site2.example.com/projects/2/pages
On looking at all the three plugins, I don't see any way for them to start loading the resources from my subdomain object, as the subdomain is not part of the route. Any advise on what I am trying to accomplish in a dry/restful way?
I don't know how to do that with resources_controller but i was able to pull off the same thing with the inherited_resources plugin.
Here is how i accomplished it:
In my application controller I set up a before_filter to find the subdomain:
def set_subdomain
#subdomain = Subdomain.find_by_url( request.host )
end
Then in my controllers Using inherited resources I set the #subdomain association using the very cool method "begin_of_association_chain"
protected
def begin_of_association_chain
#subdomain
end
Agile web development has great documentation.