how are you? I hope you're doing well, I'm telling you, I'm doing a login so that users can enter and this entry is registered, I already have the Devise gem, but I do that so that the client (company) can log in, this is for users of the company I logged in.
the error is the following:
No route matches [POST] "/sessions_contact/new"
my routes are:
Rails.application.routes.draw do
devise_for :users
root to: 'pages#home'
resources :admins
resources :clients do
resources :change_policies, only: %i[ index new create edit update destroy]
resources :age_policies, only: %i[index new create edit update destroy]
resources :refund_policies, only: %i[index new create edit update destroy]
resources :reservation_policies, only: %i[index new create edit update destroy]
resources :cancel_policies, only: %i[index new create edit update destroy]
resources :rooms, only: %i[index new create edit update destroy]
resources :contacts, only: %i[index new create edit update destroy]
resources :reservations, only: %i[index new create edit update show destroy]
end
resources :reservation, only: %i[index] do
resources :item_reservations, only: %i[index new create edit update destroy]
resources :user_reservations, only: %i[index new create edit update destroy]
resources :other_reservations, only: %i[index new create edit update destroy]
resources :obs_reservations, only: %i[index new create edit update destroy]
end
resources :registrations, only: %i[new create]
resources :sessions_contact, only: %i[index new create destroy]
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
I previously posted a similar problem and they advised me to make a new driver, which I did but it still gives me the same problem if you can help me Thank you
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
I have a weird error when I want to redirect users to the root_url when they try to access blogs/new url in my app.
My routes are
resources :blogs, only: [:index, :show] do
resources :comments, only: [:create]
end
namespace :admin do
resources :blogs
resources :users, only: [:index, :show]
resources :comments, only: [:create, :new, :destroy]
end
My non-admin blogs controller looks like this:
class BlogsController < ApplicationController
before_action :set_blog, only: [:show]
def show
unless #blog
redirect_to blogs_path
flash[:notice] = "You are not authorized to create a post."
end
end
def index
#blogs = Blog.all
end
private
def set_blog
#blog = Blog.find(params[:id])
end
end
I get the error Couldn't find Blog with 'id'=new.
In rails, the priority of routes goes from top to bottom. Meaning, when you try to hit /blogs/new, the route gets matched with the show action of blogs defined in the top of your routes.rb.
blogs/new gets matched with /blogs/:id which is mapped to blogs#show action.
In the set_blog method, params[:id] is new and since there is no record with the id of new, you're getting that weird error.
How to get around this? Change the priority of your routes.
Move the following block below the admin namespaced routes.
namespace :admin do
resources :blogs
resources :users, only: [:index, :show]
resources :comments, only: [:create, :new, :destroy]
end
resources :blogs, only: [:index, :show] do
resources :comments, only: [:create]
end
By the way, your question says that you want to avoid non-admin users to access blogs#new. If that's the case, you should try to hit /admin/blogs/new and not /blogs/new.
If you had done that, you wouldn't have gotten the error in the first place. But still, its good to know about the priority of routes in rails.
Hope this helps!
I'm having troubles with routes in Ruby on Rails. I've configured routes this way
resources :users do
collection do
resource :registrations, only: [:show, :create]
resource :sessions, only: [:new, :create, :destroy]
resource :confirmations, only: [:show]
end
end
And I have a RegistrationsController where I have two endpoints (new, create)
class RegistrationsController < ApplicationController
skip_before_filter :authenticate!
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
flash[:notice] = t("registrations.user.success")
redirect_to :root
end
end
end
But when I do rails s and I put localhost:3000/users/registrations/create or new I get a "no route matches". And I think the route exist because If I do raake routes I get this
registrations POST /users/registrations(.:format) registrations#create
GET /users/registrations(.:format) registrations#show
I know it should be a silly mistake but I don't get it. I appreciate any help
When you define routes for registrations, you're limiting it to just [:show, :create]:
resource :registrations, only: [:show, :create]
But your controller (correctly!) is presuming that there are two routes: new (to show the registration form) and create (to create the new user). You need to change your routes so that they match your controller actions:
resources :users do
collection do
resource :registrations, only: [:new, :create] # Updated this line!
resource :sessions, only: [:new, :create, :destroy]
resource :confirmations, only: [:show]
end
end
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