Create new company no longer works due to nested routes - ruby-on-rails

My app has a Company model and User models, I've been working on the path/url structure and have managed to get it working how I wish using nested resources as shown in the code below.
I've used FriendlyID to added company_names and usernames to the models which all works fine.
The path now look how I want they to look:www.mydomain.com/company_name/username
routes.rb
resources :companies, :path => '/', only: [:show] do
# constraints has been added for usernames that include a '.'
resources :users, :path => '/', only: [:show], :constraints => { :id => /.*/ }
end
PROBLEM: I still need the ability to add a new company record but it will no longer work. I understand this is due to changing the routes but I don't understand how to remedy this as it keeps viewing
www.mydomain.com/companies/new as an unknown user with the username 'new'.
I'd be grateful if anyone can point me in the right direction or give me a hard with this.

You have set only: [:show], which it means only show method is allowed.
To create a company, need to add :new and :create.
Something like this only: [:new, :create, :show]
Note:
Always after adding or changing smth in routes file, make sure to use rake routes, Rails 5 supports rails routes also.
You can see available routes!

Related

Rails Routing with Enum User Roles

I created a users table and implemented Devise. I then added an enum role to the user table so that I can identify admins, guides, and members.
What I'd like to do is limit routes for certain resources based on the enum user type. I've tried:
resources :users.guide do
get 'guide/dashboard', :to => 'guides#dashboard'
end
and some variants, but with no success. The above gives me the error:
undefined method `guide' for :users:Symbol
I've done some poking around and can't seem to find a good response. I'm avoiding CanCanCan and Rolify as I'd like to keep things as basic as possible. Any ideas? Thanks!
Edited Per Below Suggestion
So I went and updated my routes as suggested below so that the file looks similar to this:
Rails.application.routes.draw do
devise_for :users
#open to public
root 'welcome#index'
resources :guides, only: [:show], param: :username
resources :itineraries, only: [:index] #, only: [:index, :show]
authenticate :user, ->(u) { u.guide? } do
resources :guides, only: [:edit, :destroy], param: :username
get 'guide/dashboard', :to => 'guides#dashboard'
end
resources :locations, only: [:new, :create, :edit, :update, :destroy, :index, :show]
end
For whatever reason, the guide profile show/edit works fine; the dashboard works fine; but the other things outside of the authenticate block (itineraries and locations resources) don't work, I get redirected to the Devise login page. They are outside (and in one case above) the authenticate block, not sure why this is occurring with some resources/routes and not others.
You can use devise helpers. With authenticate, it is possible to make resources and routes that will ask for authentication before they can be accessed. Replacing authenticate with authenticated causes the resource to simply be unavailable if the user is not authenticated. Both helpers take an optional scope and block to provide constraints on the model instance itself.
authenticate :user, ->(u) { u.guide? } do
get 'guide/dashboard', :to => 'guides#dashboard'
end
So your problem is limit routes for certain resources based on the enum user type. I suggest you should implement the authorization stuff instead of trying to create more resources/scopes in your route, it's correctness.

Issues with URL rewriting

I want to create an online system to read a magazine's pages, so I created two scaffolds (one for the magazine itself, the other, nested, for the pages). Everything is working fine, but the url appears like the following:
domain.com/magazines/<magazine title>/pages/2/
I tried make the URL look more like /<magazine title>/<page number> by simply removing the page's class name from the URL, which didn't work
Here is my router.rb file:
root to: 'home#index'
resources :magazines do
resources :pages, except: [:index, :edit, :new]
get '/:id' => 'pages#show', :as => 'custom'
end
But when I add a link_to "custom_path", I get undefined local variable or method 'custom_path' for #<#<Class:0x00000010bb1e70>:0x00000010bb0d18>
I know I did something wrong, but where?
Thank you in advance
Run rake routes so you can get the correct path names. Then again, if you want pages#show, it's already in resources list.

RecordNotFound when viewing list of secondary resources in Refinery extension

I'm new to Ruby and Ruby on Rails. I've got an installation of Refinery CMS in which I've created two extensions that are related. I pretty much followed this guide. I've got it all working, except that for my second resource (event_types), I'm not able to view them from the front end. The link to view the Event Types goes to localhost:3000/events/event_types, and that makes the events viewer think I'm trying to look at an event with ID "event_types", and I get a RecordNotFound error. What is set up wrong? It seems like it's something wrong with routes, but I don't know what to change.
Can someone point me in the right direction?
Rails routes are matched in the order they are specified so you have to take care about order in config/routes.rb file.
For example given in guide, in file vendor/extensions/events/config/routes.rb you should put event_type resource routes above event resource route.
# Frontend routes
namespace :events do
resources :event_types, :only => [:index, :show]
end
# Frontend routes
namespace :events do
resources :events, :path => '', :only => [:index, :show]
end
To better understand Rails routes you can check http://guides.rubyonrails.org/routing.html

Rails Routing: Remove the edit suffix to url

I am creating a website with a blog module. A blog post can either be a draft or published.
A published post can no longer be edited, and a draft cannot be viewed (only edit)
I currently have a resource defined as
resources :posts, :path => "blog" do
collection do
get 'drafts'
end
end
I can access the drafts list using blog/drafts, creating new ones posts using blog/new, and editing drafts through blog/:id/edit.
However, I'd like new drafts to be created using blog/drafts/new and edited using blog/drafts/:id
I need to define the new, create, edit and update routes to use this new scheme. The new and create routes seem quite simple. However I do not know how to handle the edit route in order to remove the action name part.
Also, while looking at the default routes definition, I found in actionpack-3.2.9/lib/action_dispatch/routing/mapper.rb the following :
member do
get :edit if parent_resource.actions.include?(:edit)
get :show if parent_resource.actions.include?(:show)
[...]
end
I do not understand how rails differentiates the :edit and the :show routes, and map the urls accordingly.
Thanks
You can use the following routes. Keep in mind that it requires different file hierarchy, rake routes should be your friend in this.
namespace :blog do
resources :drafts, :controller => :posts, only: [:new, :edit]
resources :posts, only: :show
end

Overriding a resource route to / (root) in Rails3: not changing the path helper?

I am quite new to Rails3, I basically created a subscribers scaffolding, I only want my app to respond to new and create actions.
So in config/routes.rb I defined:
resources :subscribers, :only => [:new, :create]
Which works this way
GET /subscribers => subscribers#new
POST /subscribers => subscribers#create
Now I want my app to exhibit the subscribers resources at / (root) instead of /subscribers, so here is what I did:
match '/' => "subscribers#new"
match '/' => "subscribers#create"
match '/' => "subscribers#thankyou"
resources :subscribers, :only => [:new, :create]
Which somehow works, but is probably not the DRYest thing: here are the issues I have:
When going back to the form after an issue on a create the browser displays the /subscribers URL instead of just /, the form is created using the form_for(#subscriber) helper method, so the path helper must be somehow unaffected by the route
Ideally I don't even want the app to respond to a request on /subscribers
I noticed a weird bug, when posting the form while disconnected (from /, and then doing a refresh when the connection comes back (browser ask for resubmitting => OK), the Rails app crashes (I don't have the error stack though as this was on production), why is that?
Also, I tried setting up the route this way:
resources :subscribers, :only => [:new, :create] do
collection do
post '/' => :create
get '/' => :new
end
end
Which is probably DRYer, but it doesn't fix any of these issues.
I am sure this is something quite simple, please help!
Thank you for your answers, it helped me find the exact solution to my question:
resources :subscribers, :only => [:new, :create], :path => '', :path_names => {:new => ''}
Tested and working on Rails 3 :)
You could do
resources :subscribers, :path => ''
and make sure that GET / is being served by your root template, e.g. by adding this to SubscribersController:
def index
render 'welcome/index'
end
I experimented with using a match "/" declaration to override the resource index action and map it to another controller instead but apparently a resources declaration is always fully overriding manually declared routes.
For number 2 in your list, delete this line, and rewrite any _path or _url methods in your erb:
resources :subscribers, :only => [:new, :create]

Resources