link_to with double namespace - ruby-on-rails

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])

Related

No route matches for devise sessions destroy path

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

Unexpected nested route generated using Rails url_for helper

I'm using Rails 5 and my routes look something like:
Rails.application.routes.draw do
namespace :admin do
namespace :moderation do
resources :templates
end
resources :users, except: [:new, :create] do
namespace :moderation do
resources :messages, only: [:index, :show, :new, :create, :update, :edit] do
resources :message_replies, only: [:create, :edit, :update]
end
resources :templates
end
end
root "home#index"
end
end
I have models/admin/user.rb:
module Admin
class User < ::User
end
end
And models/admin/moderation/template.rb:
module Admin
module Moderation
class Template < ::Moderation::Template
end
end
end
Now when I try to generate the right route using url_for helper:
2.4.1 (main):0 > admin_user = Admin::User.first
2.4.1 (main):0 > admin_moderation_template = Admin::Moderation::Template.first
2.4.1 (main):0 > url_for([admin_user, admin_moderation_template])
NoMethodError: undefined method `admin_user_admin_moderation_template_url' for main:Object
Instead of getting admin_user_moderation_template_url I got admin_user_admin_moderation_template_url. Any help of how should I generate the right route in this case?
I wonder if you could resolve using this xxx_url() helper. In your case it'd be:
admin_user_moderation_template_url( user_id: admin_user.id, id: admin_moderation_template.id )
.
EDIT
Unfortunately I cannot try it as I would have to build the app you are working on. But as stated in this answer you can use symbols to create admin_user_moderation_template_url.
Try to run the following line and tell me if it works (I'm not sure user_id is in the right position, but it might work).
url_for( [:admin, :user, :moderation, :template, user_id: admin_user.id, id: admin_moderation_template.id ])
You need also an association between User and Template to be able to link them.

Delete/Destroy is not working within Rails

I am trying to get a delete button to work using a piece of code I have written within my tags_controller page;
def destroy
#tag = Tag.find(params[:id])
#tag.destroy
redirect_to :back, notice: 'Tag was successfully deleted!'
end
When I run from my local host, it throws an exception as shown below;
Routing Error
No route matches [DELETE] "/admin/tags/37/edit"
Rails.root: /Users/laurenwoodhams/Desktop/PROJECT/RAILS-BLOG/-t
Here is my config routes;
Rails.application.routes.draw do
get '/login' => 'admin/sessions#new'
get '/logout' => 'admin/sessions#destroy'
namespace :admin do
resources :posts
resources :tags, except: [:index]
resources :sessions, only: [:new, :create, :destroy]
resources :administrators, only: [:index, :edit, :update]
end
end
Could you please examine and explain why this may be occurring?
Thank you
DELETE is the HTTP verb (like GET or POST) which means..
To make a link to delete your tag you'll need to do something like this in your view.
<%= link_to "Delete Tag", #tag, method: "delete" %>
Then it should work properly.
P.S.
You can run bin/rake routes to see your routes.

how to use subdomain with link_to rails

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

Rails route with id after resource

I have nested routes that I'm having difficulty formatting the way I want to.
My routes.rb has the following
resource :loan_application do
scope ":loan_application_id" do
resources :wizard, only: [:show, :update]
end
end
When I click the link to create a new resource:
<%= link_to business_loan_application_path(#user), method: :post %>
I get sent to a URL that looks like the following
http://localhost:3000/businesses/69/163/loan_application/wizard/eligibility
For some reason the loan_id (163) comes before /loan_application. I would like it to come after /loan_application.
When I rake routes I can see the same problem:
business_loan_application_wizard GET /businesses/:business_id/:loan_application_id/loan_application/wizard/:id(.:format) wizard#show
In my application I made the routes without your scope:
resources :loan_application do
resources :wizard, only: [:show, :update]
end
That leads me into exact the pathes I need

Resources