0
建立電影評論網站,用戶可以上傳電影,但是當任何人想要通過點擊寫評論按鈕來評論電影時,我會在評論中發現錯誤NoMethodError#new。所以在我的代碼,它進入_form reviews.html.erbRails 4.評論中的NoMethodError#new
_form.reviews.html.erb
<%=的form_for([@電影,@review])做| F | %> - 錯誤
<% 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">
<%= f.label :rating %><br>
<%= f.number_field :rating %>
</div>
<div class="field">
<%= f.label :comment %><br>
<%= f.text_area :comment %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
審查controller.rb
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :edit, :update, :destroy]
before_action :set_movie
before_action :authenticate_user!
respond_to :html
def new
@review = Review.new
respond_with(@review)
end
def edit
end
def create
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.movie_id = @movie.id
if @review.save
redirect_to @movie
else
render 'new'
end
end
def update
@review.update(review_params)
respond_with(@review)
end
def destroy
@review.destroy
respond_with(@review)
end
private
def set_review
@review = Review.find(params[:id])
end
def set_movie
@movies = Movie.find(params[:movie_id])
end
def review_params
params.require(:review).permit(:rating, :comment)
end
end
電影控制器
class MoviesController < ApplicationController
before_action :set_movie, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@movies = Movie.all
end
def show
end
def new
@movie = current_user.movies.build
end
# GET /movies/1/edit
def edit
end
# POST /movies
# POST /movies.json
def create
@movie = current_user.movies.build(movie_params)
respond_to do |format|
if @movie.save
format.html { redirect_to @movie, notice: 'Movie was successfully created.' }
format.json { render :show, status: :created, location: @movie }
else
format.html { render :new }
format.json { render json: @movie.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /movies/1
# PATCH/PUT /movies/1.json
def update
respond_to do |format|
if @movie.update(movie_params)
format.html { redirect_to @movie, notice: 'Movie was successfully updated.' }
format.json { render :show, status: :ok, location: @movie }
else
format.html { render :edit }
format.json { render json: @movie.errors, status: :unprocessable_entity }
end
end
end
# DELETE /movies/1
# DELETE /movies/1.json
def destroy
@movie.destroy
respond_to do |format|
format.html { redirect_to movies_url, notice: 'Movie was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_movie
@movie = Movie.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def movie_params
params.require(:movie).permit(:title, :description, :movie_length, :director, :rating, :image)
end
end
耙路線
new_movie_review GET /movies/:movie_id/reviews/new(.:format) reviews#new
NoMethodError in Reviews#new
Showing /Users/neilpatel/Desktop/Rails/movies/app/views/reviews/_form.html.erb where line #1 raised:
undefined method `reviews_path' for #<#<Class:0x007f92db41b548>:0x007f92dd52ff18>
Trace of template inclusion: app/views/reviews/new.html.erb
Rails.root: /Users/neilpatel/Desktop/Rails/movies
Application Trace | Framework Trace | Full Trace
app/views/reviews/_form.html.erb:1:in `_app_views_reviews__form_html_erb__1769795460297873272_70134360199660'
app/views/reviews/new.html.erb:3:in `_app_views_reviews_new_html_erb___894596083071563840_70134360230600'
app/controllers/reviews_controller.rb:11:in `new'
寫評論按鈕
條<div class="panel panel-default">
<div class="panel-body"></div>
<div class="row">
<div class="col-md-4">
<%= image_tag @movie.image.url(:medium) %>
<div class="table-responsive">
<table class="table">
<tbody></tbody>
<tr>
<td><strong>Title:</strong></td>
<td><%= @movie.title %></td>
</tr>
<tr>
<td><strong>Description:</strong></td>
<td><%= @movie.description %></td>
</tr>
<tr>
<td><strong>Movie Length:</strong></td>
<td><%= @movie.movie_length %></td>
</tr>
<tr>
<td><strong>Director:</strong></td>
<td><%= @movie.director %></td>
</tr>
<tr>
<td><strong>Rating:</strong></td>
<td><%= @movie.rating %></td>
</tr>
</table>
<%= link_to "Write a Review", new_movie_review_path(@movie) %>
</div>
</div>
</div>
<%= link_to 'Edit', edit_movie_path(@movie) %> |
<%= link_to 'Back', movies_path %>
配置路線
Rails.application.routes.draw do
devise_for :users
resources :movies do
resources :reviews, except: [:show, :index]
end
root 'movies#index'
end
請提供完整的錯誤信息和回溯。 – 2015-02-11 11:17:13
請發表您的「寫評論按鈕」。 – spickermann 2015-02-11 11:20:08
請發佈你的'config/routes.rb'。 – 2015-02-11 11:23:17