github url style - ruby-on-rails

I wanted to have users within my website to have their own URL like http://mysite.com/username (similar to GitHub, e.g. my account is http:// github. com/sr3d). This would help with SEO since every profile is under the same domain, as apposed to the sub-domain approach.
My site is running on Rails and Nginx/Passenger. Currently I have a solution using a bunch of rewrite in the nginx.conf file, and hard-coded controller names (with namespace support as well). I can share include the nginx.conf here if you guys want to take a look.
I wanted to know if there's a better way of making the URL pretty like that.
(If you suggest a better place to post this question then please let me know)
Cheers,
Alex

Place this line right at the end of the routes.rb file, ( So that this doesn't interfere with other controller routes )
map.connect "/:username", :controller=> "users", :action => "show"
in users_controller, use the following line to fetch the user
#user = User.find_by_username(params[:username])
I don't think this would require any nginx magic or url rewrites.
HTH

Related

Rails redirect 301

I have some touble with redirect 301 in my new app. I have to redirect some old urls into the new one.
I entred in my routes file this
match "/traslochi_puglia/index.htm", :to => redirect("/preventivo/90-traslochi-in-puglia")
and it works fine, but I can't understand why this
match "/trasloco_casa_abitazione.htm", :to => redirect("/3-trasloco-casa")
does not work. All the old urls with this pattern "/some_path/page.htm" works fine but not "page.htm". Any hint?
Thanks
If you want us to troubleshoot the specific issue you've outlined in your question, we need to see your entire routes.rb file. Without this information, my first guess is this:
The typical route pattern is /controller/action or /controller/:id/action or some combination thereof. With the pattern you've shown above, and assuming you have no named routes in your routes.rb file, then the route you've provided would point to a controller, but not an action. Therefore your app wouldn't know what action to execute, unless you've specifically created a route called /3-trasloco-casa which looks to me more like a URL to a specific resource, than an action on a controller.
Getting to the source of routing issues can most easily be done with a combination of running rake routes at the command line (which shows you the list of route patterns your app will recognize), and then going further by troubleshooting with route recognition, as outlined in this answer to this question:
Recognize routes in rails console Session

Ruby on Rails and standard mod_rewrite

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.

Blank page when navigating to http://localhost:3000/say/hello

I'm new to Ruby on Rails and am doing my first tutorial and am running the latest version of rails 3 and ruby 1.9.2. After creating my controller and navigating to http://localhost:3000/say/hello I'm receiving a blank page. I do see the Welcome to Rails message when I just go to http://localhost:3000. I've done some Google searches and people have similar problems but there is no clear fix. I've never really worked with MVC before so the concept of routing is fairly new to me.
Below is my controller:
class SayController < ApplicationController
def hello
end
def goodbye
end
end
My view:
<h1>Say hello to Rails!</h1>
You should delete the public/index.html file as that will mess with your routing and display by default. Have you set up your routes already, and what is the exact location and filename of the template?
You will need something like in your config/routes.rb file to correctly route that url to your template/view:
match '/say/hello' => 'say#hello'
First delete the index.html file from your public folder. Then, go to the app/views and check the views for the say controller. You should have a hello.html.erb.
The answer to your particular question was answered already by Bitterzoet, but I thought you might want some alternative learning resources.
I'm not sure which tutorial you're starting with, but I find it odd that they're not using RESTful routes. You can find out what routes you have set up at the moment by going to the console and typing "rake routes". If you would like a different tutorial, I recommend the one here:
http://www.wiki.devchix.com/index.php?title=Rails_3_Curriculum
I'd also recommend http://railsforzombies.org/ as a good first-time rails experience.
A fun general line to add to config/routes is:
match ':controller(/:action(/:id(.:format)))'
While developing, this will allow you to display the controller/action in address bar for ALL controller/action/id.format etc.
Like Bitterzote wrote, if controller is "say" and action is "hello", http://localhost:3000/say/hello .
If you use controller "say" and action "move", http://localhost:3000/say/move .
I've found this route to be very useful during development, but change this if you launch your application! (Rails warns: "Note: This route will make all actions in every controller accessible via GET requests.")

Custom URLs in Rails of the form custom.site.com

I would like to have my users specify custom URL paths such that those paths are placed in front of my site's name, i.e. if I have a site called www.orion.com, I'd like a user to be able to create his own little home page at johnny.orion.com.
I have successfully managed to implement orion.com/johnny, which works implemented by adding map.connect ':path' at the end of my routes then making sure the path variable is present when needed.
How can I get johnny.orion.com to function?
First, you'll need to set up wildcard DNS - so that the subdomains actually resolve to somewhere.
Then, configure your virtual host to accept connections from these subdomains:
# If you're using Apache, something like:
ServerAlias *.orion.com
Then, you can use the subdomain-fu gem to handle your routes within Rails. Have a look at the associated Railscast for some good tips.
The syntax for the gem is something like this:
link_to 'Posts', post_path(#post, :subdomain => 'johnny')
johnny.orion.com/posts/4

Support for multiple domains/subdomains in Rails

I have a Rails app that has a similar setup to Tumblr, that is, you can have either:
(1) Subdomain hosting (your-username.myapp.com)
(2) Domain hosting (your-username.com)
Both would forward to a personalized website for that user, created with my application.
How can I accomplish this in Rails? I have been able to get (1) working with subdomain-fu, but I'm not sure how to get (2) working. Any pointers (plugins, gems, tutorials), etc. would be greatly helpful, I can't seem to find any.
Thanks!
The principle for domains is the same as the subdomain - find the domain, map to an account.
The details will depend on how your hosting is going to handle the DNS.
I am currently using Heroku and its wildcard service.
In this case, the domain is mapped with a cname to the subdomain hosted by my Heroku app. From here I can work out the associated account and details.
EDIT: I've found a much easier way: http://www.arctickiwi.com/blog/7-host-and-domain-based-routing-in-ruby-on-rails
Not exactly an answer but this is the best I can give. Maybe this'll help you too.
Ideally, this blog post from transfs.com and subdomain-fu should do the trick. I've been trying to implement it, however, and they don't seem to play nicely together.
Basically, if I don't include the intiializer, the subdomain route works fine. If I include the initializer, the subdomain route breaks (everything gets caught by map.root). I have a feeling it's with the way it builds the condition string in the initializer. If you can figure out how it breaks, then you'll have a working app.
My initializer:
module ActionController
module Routing
class RouteSet
def extract_request_environment(request)
env = { :method => request.method }
env[:domain] = request.domain if request.domain
env[:host] = request.host if request.host
env
end
end
class Route
alias_method :old_recognition_conditions, :recognition_conditions
def recognition_conditions
result = old_recognition_conditions
[:host, :domain].each do |key|
if conditions[key]
operator = "==="
if conditions[key].is_a?(Regexp)
operator = "=~"
end
result << "conditions[:#{key.to_s}] #{operator} env[:#{key.to_s}]"
end
end
result
end
end# end class Route
end
end
My routes (just for development). You'll see my local development domain, stiltify.dev. Sorry, I tried to make it look good in here but I couldn't get the code block to look nice. I put it on pastie instead: http://pastie.org/940619.
The comments section in Ryan Bates' screencast was very helpful, and got me to figure out the subdomain => false and the other errors they were getting into. Still didn't fix the problem though!

Resources