I have issue and I can't fix it.
This code in routes.rb
Rails.application.routes.draw do
namespace :admin do
constraints subdomain: 'admin' do
root to: "home#index"
concern :supportable do
resources :supports, only: [:new, :create]
end
resources :users, concerns: :supportable do
collection do
get 'search'
end
end
end
end
end
I want to use link_to in Ruby on Rail for link. example : <%= link_to admin_users_path do %> but in view show href="/admin/users". if I click to link redirect to http://admin.example.com/admin/users. But this link incorrect. I want to redirect link http://admin.example.com/users.
How to use link_to but render to html as href="/users".
Thanks,
Try this code with path:'/' in namespace, work for me:
Rails.application.routes.draw do
namespace :admin, path: '/' do
constraints subdomain: 'admin' do
...
end
end
end
Related
In my multilingual Rails 5 app what's the best way to forward a URL like www.myapp.com/things to www.myapp.com/things/new?
This is my routes.rb file:
Rails.application.routes.draw do
scope "(:locale)", :locale => /#{I18n.available_locales.join("|")}/ do
resources :things, :only => [:new, :create]
end
end
I tried doing this in the routes file:
get '/things', :to => redirect('/things/new')
However, I couldn't get this work with locales.
Doing this in the ThingsController does work...
def index
redirect_to new_thing_path
end
... but seems wrong since I have to create an index action in my routes file.
Thanks for any help!
I have installed devise to my rails app and was originally able to sign out using <%= link_to('Logout', destroy_user_session_path) %>
However, somewhere along the line after adding more code and a few gems (paperclip, act_as_votable, social_share_button) I was not able to use the same link. When I click the link_to I receive the error
No route matches [GET] "/users/sign_out"
I have also tried adding config.sign_out_via = :get to my devise.rb file but still got the same error. What have I done wrong?
routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: 'users/registrations', sessions: 'users/sessions' }
root to:'ideas#index'
get "/page", to: 'pages#index'
resources :ideas, only: [:index, :show, :create, :destroy, :new] do
member do
put "like", to: "ideas#upvote"
put "dislike", to: "ideas#downvote"
end
end
resources :comments, only: [:create]
end
sessions controller
class Users::SessionsController < Devise::SessionsController
def new
super
end
def create
super
end
def destroy
super
end
end
I believe <%= link_to 'Logout', destroy_user_session_path, method: :delete %> is what you're looking for
I have routes defined like this:
namespace :dealer, path: '' do
resources :images, except: :index do
resources :comments, only: [:create, :destroy] do
resources :attachments, only: :show
end
end
end
In my view using link_to url_for([#image, comment]) results in a url named:
dealer_image_dealer_comment_path which doesn't exist. It should be dealer_image_comment_path. Any way to avoid the double namespace?
I'm receiveing this error:
undefined method `dealer_image_dealer_comment_path' for #<#<Class:0x007fb599499520>:0x007fb5994980d0>
Did you mean? dealer_image_comments_path
dealer_image_comment_path
rake routes:
dealer_image_comments POST /images/:image_id/comments(.:format) dealer/comments#create {:subdomain=>"dealer"}
dealer_image_comment DELETE /images/:image_id/comments/:id(.:format) dealer/comments#destroy {:subdomain=>"dealer"}
As far as I know, with link_to you can do the following:
link_to "Comment", [#image, comment]
Take a look at link_to UrlHelper for more information
I ended up writing my own helper to accomplish this:
def path_gen(namespace, models)
path = ''
models.compact!.each do |m|
path = path + '_' + m.class.name.demodulize.downcase
end
send("#{namespace}#{path}_path", *models)
end
which accepts path_gen([#image, comment])
In my rails application I have several static and dynamic hyperlinks, programmed in the view as:
<%= link_to('Products', products_path) %>
This translates to a hyperlink like this:
<a href='http://www.mywebsite.com/products'>Products</a>
Now, I have published my API docs on subdomain api.mywebsite.com/api-docs. How can I program this link in the view so that ALL other links remain on same www root domain.
Note: If I set API-Docs link statically like this:
<a href='http://api.mywebsite.com/api-docs'>API</a>
Then all subsequent links browses on same api.mywebsite.com subdomain.
UPDATE-1:
API controller for 'products' is located as:
./app/controllers/api/v1/products_controller.rb
routes.rb looks like this after adding constraint.
Rails.application.routes.draw do
root 'pages#index'
get '/api-docs' => 'pages#api_docs'
namespace :api, defaults: {format: 'json'} do
get '/' => '/api#index'
namespace :v1 do
get '/' => '/api#index'
resources :products do
collection do
get 'search' => 'products#search', as: :search
end
end
end
end
resources :products, :only => [:index, :update, :create]
end
I have such route:
namespace admin
namespace :catalogs do
namespace :to do
resources :manufacturers do
resources :models do
resources :types do
resources :details
end
end
end
end
end
end
(please do not ask what's the model, why so man sub's: it must be so)
and such folder structure:
in manufacturer index view i have link:
= link_to "Подробнее", admin_catalogs_to_manufacturer_model_path(c), :class=>'btn btn-primary'
(also rake routes say that it is right route)
and when i try to render my index view i get:
No route matches {:action=>"show",
:controller=>"admin/catalogs/to/models",
:manufacturer_id=>#}
when i custom my link to :
= link_to "Подробнее", admin_catalogs_to_manufacturer_model_path(manufacturer_id: c.MFA_ID, id: c.MFA_ID)
i get error
uninitialized constant Admin::Catalogs::To::ModelsController
what is wrong? how to solve my routing?