I am having trouble using link_to to go to my posts/index page. I have the following link_to:
<%= link_to('NEWS', posts_path(subdomain:"news"),:class=>'navtext',:style=>"color:#bfbebc!important;") %>
My routes:
resources :posts , constraints: { subdomain: 'news' }
When I am at http://news.lvh.me:3000/posts (the actual page the link_to refers to) the link works, however when I place in on any other page (for example, the root_path) it does not work. I get the No route matches [GET] "/posts"
error which I think means that the subdomain is not passed in my link_to. Any ideas on how I can do this? Thanks.
Try posts_url
, instead of posts_path
.
It works fine on my project even without (subdomain:"news")
. Just use posts_url
directly.
If nothing else works you could just skip the helper altogether:
<%= link_to('NEWS', subdomain_link,:class=>'navtext',:style=>"color:#bfbebc!important;") %>
To make it work in both development and production you could write a helper method in application_helper.rb
:
def subdomain_link
Rails.env.development? ? '//whatever.localhost:3000' : '//news.whatever.com'
end
To go even further, you could configure subdomain_link
to accept a string as an argument and interpolate that into the domain string. Another approach would be to configure a config
variable in config/environments/development.rb
and config/environments/production.rb
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.