【问题标题】:Rails Resources new and edit routes redirecting to root page instead of their respective routesRails 资源新和编辑路由重定向到根页面而不是它们各自的路由
【发布时间】:2020-06-24 18:24:48
【问题描述】:

基本上,我正在尝试更新我的 portfolio/new 表单的布局,但是每当我输入 '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

这是我的控制器:

 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 routes 时,我可以看到正确的路径,但是当我在浏览器中输入它们时,它们不起作用。

任何帮助将不胜感激。

【问题讨论】:

  • 我的赌注是 a) 用户没有登录,或者 b) 控制器上的访问过滤器设置不正确。 access all: [:show, :index, :react], user: {except: [:destroy, :new, :create, :update, :edit]}, site_admin: :all.
  • 你是对的!我正在使用没有管理员权限的测试用户。谢谢!!

标签: ruby-on-rails ruby routes


【解决方案1】:

我认为您已恢复对 create/update/edit/destroy 的访问权限。

access_all 中为用户create/update/edit/destroy 删除except 条件

user试图打开一个没有view权限的页面时,默认是redirect_to它的root_path

【讨论】:

    猜你喜欢
    • 2020-06-15
    • 2011-01-17
    • 2014-08-22
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    相关资源
    最近更新 更多