简体   繁体   中英

Ruby on Rails: link_to is missing part of URL

I'm new to ruby on rails and I have a question concerning link_to . I have for example a link_to like this:

<%= link_to 'User...', {:controller => 'users', :action => 'userManagement'} %>

which results like this:

<a href="/users/userManagement">User...</a>

which works fine in the development environment.

But in my production environment I have an URL-rewrite before, which points to my rails server:

https://my-production-url/subdirectory

To get the link_to from above working, I need to have the subdirectory also in the path:

<a href="subdirectory/users/userManagement">User...</a>

Is it possible to configure this somewhere only for production?

I hope you understand what I mean and sorry for my bad english!

Thanks for reading, Alex

You would do this by setting action_controller.relative_url_root .

Rails.application.configure do
  # ...
  config.action_controller.relative_url_root = '/sub_directory'
end

You would place this in /config/environments/production.rb .

On a side note your route is very idiosyncratic. Normally the routes for an admin section would look like this:

Rails.application.routes.draw do
  namespace :admin, module: :admin do
    resources :users
  end
end

This would create the following routes ( $ rake routes ):

         Prefix Verb   URI Pattern                     Controller#Action
    admin_users GET    /admin/users(.:format)          admin/users#index
                POST   /admin/users(.:format)          admin/users#create
 new_admin_user GET    /admin/users/new(.:format)      admin/users#new
edit_admin_user GET    /admin/users/:id/edit(.:format) admin/users#edit
     admin_user GET    /admin/users/:id(.:format)      admin/users#show
                PATCH  /admin/users/:id(.:format)      admin/users#update
                PUT    /admin/users/:id(.:format)      admin/users#update
                DELETE /admin/users/:id(.:format)      admin/users#destroy

Think resources - not pages. And don't use camelCase in your routes or method names or you will never be allowed to sit at the cool kids table.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM