I am developing a rails app and have a question.
In my routes.rb:
map.connect 'admin', :controller => "/admin/users", :action => "index"
So when I go to "http://mydomain.com/admin", it redirects to "http://mydomain.com/admin/users/index".
However, the address remains as "http://mydomain.com/admin".
Thus, links in the page are wrong because they are created based on "http://mydomain.com/admin".
What's the solution to this problem?
Sam
try this:
map.connect 'admin/:action/:id', :controller => 'admin/users'
Your code is not redirecting the browser it's just setting up /admin and /admin/users to trigger the same action.
You could try:
map.connect 'admin', :controller => "/admin/users", :action => "redirect_to_index"
Then in your controller write:
def redirect_to_index
redirect_to :action => :index
end
This will send a redirect to the browser, causing it to display the correct URL.
Hopefully there is a better method that only involves routes.rb. This site might be helpful -> redirect-routing-plugin-for-rails
Make sure any same-domain links on the page start with a /, and use the full path. Generally you should use Rails route methods to generate your links when possible. Same goes for using the image_tag and stylesheet_link_tag helpers.
So if you have a link to "privacy.html", change it to "/privacy.html" and you should be all good no matter where in the route structure you are. This is extra nice when you start extracting your view code out to re-usable partials.
Use link_to and button_to (in UrlHelper)
Related
I use the subdomain to indicate the product eg. nexusphone.site.com . When a user types in that url I render the product/show view of that product. Users can also use domainnames eg www.nexusphone or nexusphone.com to render that product/show view.
www.site.com or site.com should render the home view which explains what my site is about.
I basically want to render the home view when www.site.com or site.com is entered. In all other cases I want to render the product/show view.
In my routes I have
map.club_root '', :controller => 'products', :action => 'show', :conditions => { :subdomain => /.+/ }
map.root :home
This works for the nexusphone.site.com example but not for nexusphone.com. Then it renders the home view.
Someone knows how I can solve this?
Thanks
maybe the problem is not related with rails...
hope the following link will help
http://www.boutell.com/newfaq/creating/withoutwww.html
cheers
With a small code change subdomain-fu can also route based on domain and host. This blog post explains it in more detail.
Right now my user profile URLs are like so:
http://example.com/users/joeschmoe
And that points to the show method in the user controller.
What I'd ideally like to do is offer user profile URLs like this:
http://example.com/joeschmoe
So, what sort of route and controller magic needs to happen to pull that off?
I disagree with what jcm says about this. It's not a terrible idea at all and is used in production by the two biggest social networks Facebook and MySpace.
The route to match http://example.com/username would look like this:
map.connect ':username', :controller => 'users', :action => 'show'
If you want to go the subdomain route and map profiles to a URL like http://username.example.com/, I recommend using the SubdomainFu plugin and the resulting route would look like:
map.root :controller => 'users', :action => 'show' , :conditions => {:subdomain => /.+/}
These broad, catch all routes should be defined last in routes.rb, so that they are of lowest priority, and more specific routes will match first.
I also recommend using a validation in your User model to eliminate the possibility of a user choosing a username that will collide with current and future routes:
class User < ActiveRecord::Base
validates_exclusion_of :username, :in => %w( messages posts blog forum admin profile )
…
end
This does not make sense unless you have no controllers. What happens when you want to name a controller the same as an existing user? What if a user creates a username the same as one of your controllers? This looks like a terrible idea. If you think the /user/ is too long try making a new custom route for /u/
So your custom route would be...
map.connect 'u/:id', :controller => 'my/usercontroller', :action => 'someaction'
In routes.rb this should do the trick:
map.connect ":login", :controller => 'users', :action => 'show'
Where login is the name of the variable passed to the show method. Be sure to put it after all other controller mappings.
Well, one thing you need is to ensure that you don't have name collisions with your users and controllers.
Once you do that you, can add a route like this:
map.connect ':username', :controller => 'users', :action => 'show'
Another thing people have done is to use subdomains and rewrite rules in the web server, so you can have http://joeshmoe.example.com
In Rails 4 to skip controller from url you have to do add path: ''.
resources :users, path: '' do
end
Let's say I have a Ruby on Rails blogging application with a Post model. By default you would be able to read posts by http://.../post/id. I've added a route
map.connect ':title', :controller => 'posts', :action => 'show'
that will accept http://.../title (titles are unique) and the controller will do a query for the title and display the page. However when I am now calling <%= link_to h(post.title), post %> in a view, Rails still gives me links of the type post/id.
Is it possible to get Rails to automatically create the pretty links for me in this case?
If you are willing to accept: http:/.../1234-title-text you can just do:
def to_param
[id, title.parameterize].join("-")
end
AR::Base.find ignores the bit after the id, so it "just works".
To make the /title go away, try naming your route:
map.post ':id', :controller => 'posts', :action => 'show', :conditions => {:id => /[0-9]+-.*/ }
Ensure this route appears after any map.resources :posts call.
You can override ActiveRecord's to_param method and make it return the title. By doing so, you don't need to make its own route for it. Just remember to URL encode it.
What might be a better solution is to take a look at what The Ruby Toolbox has to offer when it comes to permalinks. I think using one of these will be better than to fixing it yourself via to_param.
I would use a permalink database column, a route, and I normally skip using link_to in favor of faster html anchor tags.
Setting your route like:
map.connect '/post/:permalink', :controller => 'post', :action => 'show'
then in posts_controller's show:
link = params[:permalink]
#post = Post.find_by_permalink(link)
You link would then be
Link
then in your create method, before save, for generating the permalink
#post = Post.new(params[:post])
#post.permalink = #post.subject.parameterize
if #post.save
#ect
There is a Gem for you to get this done perfectly
https://github.com/rsl/stringex
I'm struggling here with a problem:
I have a controller questions which has action new.
Whenever I need to create new question, I'm typing
/questions/new
What changes to routes.rb should I make to change the URI to
/questions/ask
Thank you.
Valve.
Try this:
map.ask_question '/questions/ask', :controller => 'questions', :action => 'new'
Then you'll have a named route and you can:
link_to "Ask a question", ask_question_path
If you are using RESTful routes maybe you'd like to use map.resources for your questions.
To rename the action urls you may do this:
map.resources :questions, :path_names => { :new => 'ask', :delete => 'withdraw' }
(I added delete for the sake of the example)
Which version of rails?
Generally the default route should catch anything like /:controller/:action, so you could just create an ask method in your questions controller. Take a look at the api documentation for named_route and map_resource if you want something a bit smoother to work with.
I'm following this tutorial (seems good) for Rails. After I run
ruby script/generate scaffold Post
then this link works in one of the erb files:
<%= link_to "My Blog", posts_path %>
WHY? I've looked for "posts_path" in the whole app and it's nowhere to be found. On the other hand, this
<%= link_to "My Blog", home_path %>
does not work, and it's also a Controller.
Where is the posts_path defined?
posts_path is a named route you get for free from the route that was added by script/generate scaffold. See routes.rb you should see something like this:
map.resources :posts
See the API docs for information on what other named routes you get for free.
Also you can run rake routes and see what all your routes.rb is giving you.
If you want a home_path named route add a line like this to your routes.rb:
map.home '/home', :controller => "home", :action => "index"
I believe that "posts_path" is created dynamically by Rails at runtime. Look at your routes.rb file - Home is probably not defined the same way as Posts. It has nothing to do with you controllers, it's dependent on the route definition.
map.root :controller => "home" would be a shorter way of writing the path to your home directory. This will use / has the home, and not /home. If you still want to use /home (and home_path), map.home 'home', :controller => "home" will do the same thing.
There's a great guide written by Mike Gunderloy about everything there is to know about routing.