In my file routes.rb
namespace :admin do
resources :albums, except: [:new, :edit, :destroy]
resources :conversation_replies, except: [:new, :edit, :destroy]
resources :authors, except: [:new, :edit, :destroy]
end
Now, I want delete line "resources :authors, except: [:new, :edit, :destroy]" by code in ruby on rails
please help me. thank you very much
It's a text file. It's not generated programmatically, it's meant to be edited by hand. You just delete the line. Open your editor, select the line, and hit the backspace or delete key.
Related
On a checkout controller if the user did not fill in their address he should be redirected to the address path, but after the redirect rails is outputting:
undefined method `new_user_address_path' for #
The checkout code:
def checkout
#order = current_user.cart.orders.find(params[:id])
if current_user.address.blank?
redirect_to new_user_address_path(current_user)
flash[:error] = 'Antes de prosseguir por favor, preencha o seu endereço'
end
end
I added a helper method but it did not worked. Why is the controller warning the path as a method?
Routes
user address
user_address POST /users/:user_id/address(.:format) address#create
new_user_addres GET /users/:user_id/address/new(.:format) address#new
checkout
orders GET /orders(.:format) orders#index
order PUT /orders/:id(.:format) orders#update
DELETE /orders/:id(.:format) orders#destroy
The problem is here
resources :users, only: [:new, :create, :edit, :update, :show] do
resources :address, only: [:new, :create]
end
By default the resources method with an s considers the first argument to be a plural so it will try to remove the extra s for generating member urls, hence the new_user_addres path.
In order to fix this I suggest renaming
resources :users, only: [:new, :create, :edit, :update, :show] do
resources :address, only: [:new, :create]
end
to
resources :users, only: [:new, :create, :edit, :update, :show] do
resources :addresses, only: [:new, :create]
end
And adding the irregular plural to your inflections
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'address', 'addresses'
end
Note that you will have to restart your server
Given the following URL design for a nested resources in a RESTful API:
/magazines/:magazine_id/ads/:id POST
What's the rational behind having the magazine id in there given that the ads id uniquiely identifies ads across magazines?
Besides that it looks maybe nicer when presenting the user with that URL or simply convention. Is there any deeper meaning or constraint?
Well, this depends very much on who is developing. In theory, there's no need.
In fact, Rails Guides show this in the 2.7.2 Section (Shallow Nesting), that you can nest the resources only when they don't have an id:
resources :articles do
resources :comments, only: [:index, :new, :create]
end
resources :comments, only: [:show, :edit, :update, :destroy]
Or in your case:
resources :magazines do
resources :ads, only: [:index, :new, :create]
end
resources :ads, only: [:show, :edit, :update, :destroy]
given that the ads id uniquiely identifies ads across magazines?
This is the most common convention, but not a universal one. You are free to override to_param in your model and violate the universality-by-database-primary-key convention. Imagine that you also did this for magazines (e.g. for SEO purposes). In this case, including the magazine ID/slug in the route might be quite necessary.
This is exactly what "shallow" does:
resources :magazines do
resources :ads, shallow: true
end
means exactly the same as
resources :magazines do
resources :ads, only: [:index, :new, :create]
end
resources :ads, only: [:show, :edit, :update, :destroy]
See section 2.7.2 Shallow Nesting in http://edgeguides.rubyonrails.org/routing.html
I have the following routes
resources :eclubs, except: [:show]
namespace :eclubs do
resources :leaders, only: [:index, :show, :new, :create, :destroy]
resources :members, only: [:index, :show, :new, :create, :destroy]
end
However, /eclubs/members does not routes to the index action of the Eclubs::Members controller. Instead it routes to the show action of the Eclubs controller. How do I fix this?
Because it is declared first, the Eclubs controller is being given precedence over the Eclubs::Members. Instead declare the Eclubs controller's routes last e.g.
namespace :eclubs do
resources :leaders, only: [:index, :show, :new, :create, :destroy]
resources :members, only: [:index, :show, :new, :create, :destroy]
end
resources :eclubs, except: [:show]
I am trying to create the following Routes
/plans
/plans/:plan_id/notes
/plans/:plan_id/notes/:id
/notes/:note_id/reply
/notes/:note_id/upvote
This is my Route file
resources :plans, only: [:create, :index, :show] do
resource :note, only: [:create]
resources :notes, only: [:destroy]
end
resources :notes, only: [:index] do
resource :note_replies, only: [:create, :destroy]
resources :note_upvotes, only: [:create, :destroy]
end
Is there a way to remove duplication of routes created for notes?
I am using devise and therefore do not need a users controller.However, i also need nested routes and my config.routes looks like this;
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
devise_for :users
resources :users do
resources :personal_accounts,path: "user_account", only: [:show] do
resources :deposits, only: [:new, :show, :create, :index]
resources :withdraws, only: [:new, :show, :create, :index]
end
resources :businesses do
resources :business_accounts, path: "business_account", only: [:show] do
resources :business_withdraws, only: [:new, :show, :create, :index]
resources :business_deposits, only: [:new, :show, :create, :index]
end
end
end
How can i go past this error while also maintaining my nested routes.
Thank you.
You have three levels of nested routes there, which is normally considered to be undesirable: http://edgeguides.rubyonrails.org/routing.html#nested-resources
Resources should never be nested more than 1 level deep.
This bit resources :users do will create all the named routes for the users controller, which I suspect is where your error comes from. Why do you need this? Better perhaps to specify the routes without it?
resources :personal_accounts,path: "user_account", only: [:show] do
resources :deposits, only: [:new, :show, :create, :index]
resources :withdraws, only: [:new, :show, :create, :index]
end