我使用脚手架创建了一个用户可以创建字母的应用程序(新手)。
在添加其他视图('app / views / letters / review.html.erb')以查看字母创建时,我遇到了尝试重定向的路由问题
redirect_to review_path, notice: 'Letter was successfully created.'
通过我认为现在应该工作的路径:
match '/review', to: 'letter#review', via: 'get'
我做错了什么?
完整设置,控制器:
class LettersController < ApplicationController
before_action :set_letter, only: [:show, :edit, :update, :destroy, :review]
def review
end
...
def create
@letter = Letter.new(letter_params)
@letter.user_id = current_user.id if current_user
respond_to do |format|
if @letter.save
format.html { redirect_to review_path, notice: 'Letter was successfully created.' }
format.json { render action: 'show', status: :created, location: dashboard_path }
else
format.html { render action: 'new' }
format.json { render json: @letter.errors, status: :unprocessable_entity }
end
end
end
路线:
Appname::Application.routes.draw do
resources :letters
resources :charges
devise_for :users
get 'letter/new'
root 'static_pages#home'
match '/review', to: 'letter#review', via: 'get'
match '/dashboard', to: 'account#dashboard', via: 'get'
match '/order', to: 'account#order', via: 'get'
答案 0 :(得分:0)
更新路线如下:
match '/review', to: 'letters#review', via: 'get'
您的控制器名称为LettersController
,请注意复数letters
。因此,在定义路线时,您需要指定复数letters
。
您收到uninitialized constant error
因为在审核路线中指定控制器时使用了letter
(单数),因此Rails路由器开始寻找不存在的LetterController
。因此,你得到了错误。