My .net application has a url that ends with .aspx like:
www.example.com/users/find.aspx?userid=234234
I don't want to re-write the url, I want to keep it as is.
Is it possible for a Rails 3 application (nginx, passenger) to respond to this request using a customized route?
Yes - you need to update your routes file (config/routes.rb).
See:
http://guides.rubyonrails.org/routing.html#non-resourceful-routes
Sure, just add a route to config/routes.rb. In Rails 3 that would like:
match 'users/find.aspx' => 'users#find'
Related
What regex is used in Rails routes such that it works cross environments. I am trying to setup a middleware such that requests coming to /:id:/show/details does a reverse proxy to wordpress.com/:id, was wondering if I can use something Rails has already built-in for this use case
You can always do something like this:
get '/:id/show/details', to: redirect{|params| "https://wordpress.com/mypage/#{params[:id]}"}
I have a messy rails 3 application that's come from a different developer and I need to refactor it.
What I want to do is move the contents of "app" into a subfolder called "classic".
app/classic
And then have all URL's with a classic prefix such as
localhost:3000/classic/wills/new
Route to controllers inside of the "app/classic" folder.
And then every regular url that does not contain the classic prefix - route in the standard way to app/
Is it possible to do this? The only thing I've discovered so far is that I can add a scope inside of my routes file.
scope(:path => '/classic')
But all that does is require a prefix for every URL. I'm really not sure how to go about this!
This is a route namespace. Take a look at this section in Rails Routing from the Outside In: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
namespace :classic do
# your routes here
end
This will do 3 things -
the path to the controller files need to be under /app/controllers/classic/
the name of the controllers need to change to Classic::ControllerName
the url is now /classic/controller/action
This sounds like what you want, but you can modify this to get just the parts you want if you don't want all 3.
In route.rb file:
#Of course, you have to configure the right http method.
get 'wills/new' => 'wills#new', as: 'to_classic_wills_new'
Hope this helps!
If I want to set up simple CGI application which would work on any request (say / or /hello/, /bye and so on I would have to set up, for example, .htaccess file which would redirect any request to my script.rb
But how do all requests end up in routes.rb or in another words - where is the starting point of Rails application?
Here is a presentation about request life cycle in Rails http://www.slideshare.net/lachie/the-rails-request-cycle
This is quite good overview how Rails dispatcher works: http://brainspl.at/request_response.pdf
I've got an old application written in PHP and now I am replacing it by new RoR app. The old application has links like "this-is-seo-title,n123.html". In htaccess there is a rule which is translating those links to news.php?id=123.
Now when I setup RoR app, links are in "RoR way" (:controller/:action/:id). It's cool and nice, but in Google I've got about 50k indexed subpages. I don't want get this indexed subpages broken so now here is a question:
Can I create new rules inside htaccess file which will be translating "this-is-seo-title,n123.html" links to /news/123 ?
I didn't deploy app yet and I don't have access to environment with passenger module, so I can't test it myself.
I don't think it's necessary to use htaccess. If you are going rails, then do it with the rails helpers. I think this should work in your routes.rb:
match 'this-is-seo-title,n:id.html' => 'news#show'
That route will invoke the NewsController and the show action with 123 as an :id parameter. Was that what you where looking for?
Edit:
For Rails 2
map.connect 'this-is-seo-title,n:id.html', :controller => 'news', :action => 'show'
At least I think that will work in Rails 2. I don't have any environment up and running atm to test with. Let me know if it doesn't work.
I need to write the full path so need to know what the rails_root domain is. How do I do that? For example:
string = "{RAILS_ROOT}/vendors/#{#vendor.id}"
What is the equivalent of "RAILS_ROOT" to give me what the full domain is for my application? So that in development it would subsstitute localhost:3000 and on my heroku site the right full domain?
You should always avoid, if possible, hard-coding your path, because it is less flexible and more prone to result in broken links in the future. Plus, you can use Rails routing, which is an elegant way to generate everything cohesively in Rails without any need to create the composite parts yourself.
If you have your routes set up properly, you should be able to call:
link_to "View vendor", vendor_url(#vendor.id)
Vendor_url(#vendor.id) in Rails gives you your full URL, which you can then contain in your string variable. Here's how to generate the routes needed for the above:
# in routes.rb
resources :vendors
Try:
File.realpath(RAILS_ROOT)
You could access the request object. request.host_with_port would give you the hostname and port. request.protocol will give you the protocol (http:// or https://). request.fullpath will give you the path with query params.