[英]Rails Resources new and edit routes redirecting to root page instead of their respective routes
基本上,我正在尝试更新我的投资组合/新表单的布局,但是每当我输入'localhost:3000/portfolios/new'时,我都会被重定向到我的主页。 与'localhost:3000/portfolios/(:id)/edit'相同
我的路线如下:
Rails.application.routes.draw do
#Devise Routes
devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout', sign_up: 'register' }
#homepages routes
get 'about-me', to: 'pages#about'
get 'contact', to: 'pages#contact'
# blog
resources :blogs do
member do
get :toggle_status
end
end
#portfolio
resources :portfolios, except: [:show]
get 'portfolio/:id', to: 'portfolios#show', as: 'portfolio_show'
get 'react-items', to: 'portfolios#react'
# setting root path --> ex: localhost:3000/
root to: 'pages#home'
end
这是我的 controller:
class PortfoliosController < ApplicationController
before_action :set_portfolio_item, only: [:edit, :update, :show, :destroy]
layout "portfolio"
access all: [:show, :index, :react], user: {except: [:destroy, :new, :create, :update, :edit]}, site_admin: :all
def index
@portfolio_items = Portfolio.all
end
# custom scope
def react
@react_portfolio_items = Portfolio.react
end
def show
end
def new
# new portfolio item is initialized.
@portfolio_item = Portfolio.new
3.times { @portfolio_item.technologies.build }
end
def create
@portfolio_item = Portfolio.new(portfolio_params)
respond_to do |format|
if @portfolio_item.save
format.html { redirect_to portfolios_path, notice: 'Portfolio Item was successfully created.' }
else
format.html { render :new }
end
end
end
def edit
end
def update
respond_to do |format|
if @portfolio_item.update(portfolio_params)
format.html { redirect_to portfolios_path, notice: 'Portfolio Item was successfully updated.' }
else
format.html { render :edit}
end
end
end
def destroy
# destroy the record
@portfolio_item.destroy
# redirect
respond_to do |format|
format.html { redirect_to portfolios_url, notice: 'Record was removed.' }
end
end
private
def portfolio_params
params.require(:portfolio).permit(:title,
:subtitle,
:body,
technologies_attributes: [:name]
)
end
def set_portfolio_item
@portfolio_item = Portfolio.find(params[:id])
end
end
所以总的来说,我不确定它为什么会这样做。 当我做rails 路线时,我可以看到正确的路径,但是当我在浏览器中输入它们时,它们不起作用。
任何帮助将不胜感激。
我认为您已恢复对create/update/edit/destroy的访问权限。
从access_all
中删除用户create/update/edit/destroy
的except
条件
当user
试图打开一个没有view
权限的页面时,默认情况下它会redirect_to
到它的root_path
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.