我想寫一個新的評論嵌套在resturants,但我不斷收到評論#新行動沒有方法錯誤。我不斷收到一條紅線評論形式NoMethodError in評論#新
<%= form_with(model: [@review, @resturant] , local: true) do |form| %>
完整的錯誤消息:
NoMethodError in Reviews#new Showing /Users/AHmed/Desktop/burgerland-ar/app/views/reviews/_form.html.erb
其中線#1提出:
undefined method `review_resturant_path' for #<#<Class:0x007f8b3abe3170>:0x007f8b3abe04e8> Did you mean? resturant_reviews_path resturant_review_path new_resturant_path
做了新軌道5語法改變或錯誤與評論控制器?這裏是我的代碼:
評論/ _form.html.erb
<%= form_with(model: [@review, @resturant] , local: true) do |form| %>
<% if review.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(review.errors.count, "error") %> prohibited this review from being saved:</h2>
<ul>
<% review.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :rating %>
<%= form.number_field :rating, id: :review_rating %>
</div>
<div class="field">
<%= form.label :comment %>
<%= form.text_area :comment, id: :review_comment %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
/reviews_controller.rb
class ReviewsController < ApplicationController
before_action :set_review, only: [:edit, :update, :destroy]
before_action :set_resturant
before_action :authenticate_user!
# GET /reviews/new
def new
@review = Review.new
end
# GET /reviews/1/edit
def edit
end
# POST /reviews
# POST /reviews.json
def create
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.resturant_id = @resturant.id
respond_to do |format|
if @review.save
format.html { redirect_to root_path, notice: 'Review was successfully created.' }
format.json { render :show, status: :created, location: @review }
else
format.html { render :new }
format.json { render json: @review.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reviews/1
# PATCH/PUT /reviews/1.json
def update
respond_to do |format|
if @review.update(review_params)
format.html { redirect_to @review, notice: 'Review was successfully updated.' }
format.json { render :show, status: :ok, location: @review }
else
format.html { render :edit }
format.json { render json: @review.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reviews/1
# DELETE /reviews/1.json
def destroy
@review.destroy
respond_to do |format|
format.html { redirect_to reviews_url, notice: 'Review was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_review
@review = Review.find(params[:id])
end
def set_resturant
@resturant = Resturant.find(params[:resturant_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def review_params
params.require(:review).permit(:rating, :comment)
end
end
耙路線
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
resturant_reviews POST /resturants/:resturant_id/reviews(.:format) reviews#create
new_resturant_review GET /resturants/:resturant_id/reviews/new(.:format) reviews#new
edit_resturant_review GET /resturants/:resturant_id/reviews/:id/edit(.:format) reviews#edit
resturant_review PATCH /resturants/:resturant_id/reviews/:id(.:format) reviews#update
PUT /resturants/:resturant_id/reviews/:id(.:format) reviews#update
DELETE /resturants/:resturant_id/reviews/:id(.:format) reviews#destroy
resturants GET /resturants(.:format) resturants#index
POST /resturants(.:format) resturants#create
new_resturant GET /resturants/new(.:format) resturants#new
edit_resturant GET /resturants/:id/edit(.:format) resturants#edit
resturant GET /resturants/:id(.:format) resturants#show
PATCH /resturants/:id(.:format) resturants#update
PUT /resturants/:id(.:format) resturants#update
DELETE /resturants/:id(.:format) resturants#destroy
root GET / resturants#index
pages_about GET /pages/about(.:format) pages#about
pages_help GET /pages/help(.:format) pages#help
的routes.rb
Rails.application.routes.draw do
devise_for :users
resources :resturants do
resources :reviews , except: [:index,:show]
end
root 'resturants#index'
get 'pages/about'
get 'pages/help'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
安置自己的'耙routes'導致 –
我已經添加了耙路線 – AHmed
請張貼完整的錯誤消息 – Pavan