Reputation: 4492
In my Rails project, in my index
view, I have a link
<%= link_to 'Show all posts', show_all_path %>
In routes.rb
, I have a route:
match "show_all" => "Posts#show_all"
When I click on that link, it goes from
http://<domain name>/my_rails_project
to
http://<domain name>/my_rails_project//show_all
It works. However, having two forward slashes doesn't look very nice. Can I make it so that only one forward slash appear?
EDIT: These are some of my files:
config/environment.rb
require File.expand_path('../application', __FILE__)
Blog::Application.initialize!
config/environments/development.rb
Blog::Application.configure do
config.cache_classes = false
config.whiny_nils = true
config.consider_all_requests_local = true
config.action_view.debug_rjs = true
config.action_controller.perform_caching = false
config.action_mailer.raise_delivery_errors = false
config.active_support.deprecation = :log
config.action_dispatch.best_standards_support = :builtin
config.action_controller.asset_host = "//pauls.scripts.asu.edu/blog/public"
end
config/routes.rb:
Blog::Application.routes.draw
do resources :posts match "show_all" => "Posts#show_all"
end
Output of rake routes
show_all /show_all(.:format) {:action=>"show_all", :controller=>"Posts"}
Upvotes: 2
Views: 2156
Reputation: 633
This is not a rails issue. Rails do not know his own url. It knows only relative url of resources. These relatives urls are appened to the domain url. It does no generate an illegal url, so no error is raised.
So it look likes a virual host problem. But i can't tell you what is exactly the problem. At some point your domain url is www.your_domain/rails_projects/.
You may change that, by updating your virtual host in order to get www.your_domain.
Update :
Here an example :
<VirtualHost 172.20.30.40>
DocumentRoot /www/subdomain/sub2
ServerName www.sub2.domain.tld
ServerPath /sub2/
</VirtualHost>
will respond to :
http://www.sub2.domain.tld/sub2/
It looks like pretty similar to your problem.
Upvotes: 1
Reputation: 419
Try to reproduce this with a blank application and post all the code on github, i think the error could be something else.
Please tell us your rails version and ruby version.
Upvotes: 0
Reputation: 9623
How are you setting the app to be served under a sub-uri? That's the most likely issue. Perhaps report back on exactly how you are doing that. Presumably you are not running this under webrick but under passenger on a server in dev mode?
What is the value of RailsBaseURI, or RAILS_RELATIVE_URL_ROOT, depending on what you are using, does it have a trailing slash? Check your apache/passenger config for trailing slashes anywhere on folders (document root in the virtual host too?). Try also outputting the value of root_url to see what that is.
As a simple test locally, make a new project and use a route something like this (note the scope):
Blog::Application.routes.draw do
scope 'my_rails_project' do
match '/test', :to => 'welcome#test'
end
end
And then a test action in a new posts controller:
def test
render :text => "ROOT-#{root_url}"
end
That might shed some light on what is going wrong, by using simpler routes, and walking you through redoing the setup.
Then on your actual project, two things you can try, changing the ENV variable with which you are setting up the sub-uri, and verifying virtual host setup, and setting up a scope around all of your routes:
scope 'my_rails_project' do
match '/show_all'=> "posts#show_all", :as => "show_all"
end
I would also try inspecting the output of show_all_path, to see what you're actually getting in your link, for example are you getting '//' in the href, or something else?
Upvotes: 1
Reputation:
If you want an url like http://<domain name>/my_rails_project/show_all
add this to your routes.rb
resources :posts
match "show_all", :to => "posts#show_all"
If you prefer an url like http://<domain name>/my_rails_project/posts/show_all
add this:
resources :posts do
collection do
get "show_all"
end
end
You can see more examples here
Upvotes: 0
Reputation: 851
Did you tried this syntax?
match '/show_all'=> "posts#show_all", :as => "show_all"
:as option gives you correct path
<%= link_to 'Show all posts', show_all_path %>
Or maybe your default_url_options appending extra slash to end of the url. Check your action controller.default_url_options
Upvotes: 0
Reputation: 2743
I notice that your Rails app is in a subdirectory. I'm not sure if it is related or not, but a quick search produced this guide which may or may not solve your problem.
Upvotes: 0
Reputation: 23
when I use "match" this is the syntax that I use:
match "/show_all" => "Posts#show_all"
However I'm not sure if that forward slash would solve your problem. Give it a try?
Cheers
Upvotes: 1