Create a link to a nested resource where the parent does not have resource name in the URL - ruby

I have setup my routes to work without the resource in the url (i.e. /username/posts, as opposed to /users/username/posts) - using friendly_id gem inorder to use :username instead of :id.
The resources are under a namespace (cpanel) and work fine.
My routes are setup like this:
namespace :cpanel do
resources :users, :path => '', :constraints => { :id => /[\w+\-\_]+/ } do
resources :posts
end
end
Navigating to /cpanel/username/posts works fine, but I am having trouble setting up my link_to I am using the path cpanel_user_posts_path(#user) but this creates a link to the URL /cpanel/users/:username/apps.
How can I create a link to the path: /cpanel/:username/apps?
Thanks in advance.

Did you try something like this:
get 'cpanel/:username/apps', to: 'controller#action
You can always hard code it (as a last resort) via:
link_to "Apps", "/#{current_user.username}/apps"

Related

Could not find devise mapping for path

I have the following routes defined in my routes.rb
devise_for :users, controllers: { registrations: "network/registrations",sessions: 'network/sessions', passwords: 'network/passwords' }
devise_scope :user do
get "registrations/show" => "network/registrations", as: :show_user_profile
end
and when i do rake routes i also see
network_show_user_profile GET /network/registrations/show(.:format) network/registrations#show
But when i try to access the path /network/registrations/show
i get the below exception
Could not find devise mapping for path "/network/registrations/show".
This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example:
devise_scope :user do
get "/some/route" => "some_devise_controller"
end
2) You are testing a Devise controller bypassing the router.
If so, you can explicitly tell Devise which mapping to use:
#request.env["devise.mapping"] = Devise.mappings[:user]
I tried to modify the routes.rb and added
devise_scope :user do
get 'user_profile' => 'registrations#search', :as => 'user_profile'
end
and when i access user_profile path, i get the error
The action 'show' could not be found for Network::RegistrationsController
but when i add the action show to the controller I again get the same exception message
Could not find devise mapping for path "/network/registrations/show".
This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example:
devise_scope :user do
get "/some/route" => "some_devise_controller"
end
2) You are testing a Devise controller bypassing the router.
If so, you can explicitly tell Devise which mapping to use:
#request.env["devise.mapping"] = Devise.mappings[:user]
any help as to what i am doing wrong would be appreciated. Thanks.
I tried adding same routes that you did
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "network/registrations",sessions: 'network/sessions', passwords: 'network/passwords' }
devise_scope :user do
get "registrations/show" => "network/registrations", as: :show_user_profile
end
end
But instead of
network_show_user_profile GET /network/registrations/show(.:format)
I got show_user_profile GET /registrations/show(.:format) registrations#show
So I replaced this routes file with below
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "network/registrations",sessions: 'network/sessions', passwords: 'network/passwords' }
devise_scope :user do
get "/network/registrations/show" => "network/registrations#show", as: :show_user_profile
end
end
I also created an action show in network/registrations_controller.rb and
everything works perfectly for me.
Hope this helps.

Rails Nameerror with double namespace controller, namespaced model

Can't work out how to get around this. I'm using the mailboxer gem, who have recently updated to a namespaced model. Using the main controller, everything is fine, but I also have a namespaced views/controller for an admin section which is causing the issue.
Model name is
mailboxer_conversations
Main section code (working fine):
routes.rb
namespace :mailboxer, path: '', as: nil do
resources :conversations, only: [:index, :show, :new, :create, :destroy], as: 'conversations', path: 'conversations' do
member do
post :reply
post :trash
post :untrash
end
end
controllers/mailboxer/conversations_controller.rb
class Mailboxer::ConversationsController < ApplicationController
end
The above is all working fine, the controller is namespaced with folders and I can access the Conversation model instance.
The below is the code I'm currently trying for the admin section, using the same model mailboxer_conversations.rb
routes.rb
namespace :admin do
namespace :mailboxer, path: '', as: nil do
resources :conversations, as: 'conversations', path: 'conversations'
end
end
controllers/admin/mailboxer/conversations_controller.rb
class Admin::Mailboxer::ConversationsController < ApplicationController
end
With the above setup, I'm getting a nameerror uninit. constant 'Conversations'. Says to me that it can't access the model, is this because of the double namespace, it's expecting the model.rb file to be in a different folder i.e admin/mailboxer_conversations.rb? I can't move the model, as it's in a gem.
Thanks

How to write specific custom routes for rails + devise?

I am trying to write a custom route that will point to a custom controller action in devise.
I have the setup below right now.
# custom controller
class Registrations::RegistrationsController < Devise::RegistrationsController
layout 'settings'
# GET /resource/edit
def edit
super
end
end
# routing setup
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "registrations/registrations" },
path_names: { edit: 'profile' }
end
This allows me to have a custom URL localhost:4000/users/profile with no problems.
My question is how can I customize this further to be
localhost:4000/profile
localhost:4000/settings/profile
Note I know that I can set path: '' or path: 'settings', but that will affect all routes within users.
Is there a way that I could have
localhost:4000/settings/profile and localhost:4000/login at the same time using devise_for?
I am not sure how to control these affects separately.
As we can see here, we can use Rails scopes and specify a controller for 'registration', for example. Something like this:
scope :settings do
devise_scope :user do
get '/profile' => 'devise/registrations#edit'
end
end

easy way to rename URL in Rails?

I just want to make a change to the URL without getting an undefined method error. I can try to do it manually but is there an easy way to do so?
For example, on my local server, if the URL is:
/users/username/XYZ
Is there an easy way to change "XYZ" to something else easily?
If XYZ is a part of a resourceful route you may modify it like this
resources :users, :path_names => { :new => 'make', :edit => 'change' }
OR
if you want to change the name of the resource only you may change it like this:
resources :person, :controller => 'users', :as => 'users'
this would change the urls to 'person' and maintaining all class and method names.
Here is the rake routes output:
users GET /person(.:format) users#index
POST /person(.:format) users#create
new_user GET /person/new(.:format) users#new
edit_user GET /person/:id/edit(.:format) users#edit
user GET /person/:id(.:format) users#show
PUT /person/:id(.:format) users#update
DELETE /person/:id(.:format) users#destroy
You can find out more on how to customize resourceful routes here:
http://guides.rubyonrails.org/routing.html#customizing-resourceful-routes
I think this will be exactly what u are looking for :) ...
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
http://railscasts.com/episodes/63-model-name-in-url-revised
Regards

Paperclip generating wrong URLs in Heroku

Paperclip is generating wrong URLs in Heroku.
I have an Audio model which has a mp3 field as follows:
class Audio < ActiveRecord::Base
has_attached_file :mp3,
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:bucket => S3_CREDENTIALS[:bucket],
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
I am calling audio.mp3.url from a controller, and it returns
http://s3.amazonaws.com/MyApp/audios/mp3s//original/96a9ae89302fdf8462ee05eb829f2e17578b144e20120908-2-11f61zr.mp3?1347135050
instead of
http://s3.amazonaws.com/MyApp/audios/mp3s/000/000/004/original/96a9ae89302fdf8462ee05eb829f2e17578b144e20120908-2-11f61zr.mp3?1347135050
(which works)
Why is it missing the '000/000/004' part of the route?
The same model is generating the right URL when used in a view.
Any help?
I am using paperclip 3.2.0 and Rails 3.1.8.
Any help?
It looks like the '000/000/004' part of the route is the new to paperclip as of 3.0 feature of :id_partition. Try changing :id to :id_partition. Maybe you changed url in your code after already having uploaded something?

Resources