我正在构建一个相当简单的专辑评论应用程序。我正在尝试为新评论设置simple_form。改变不会坚持下去。这是我尝试添加评论时在终端中显示的内容:
Started POST "/albums/6/reviews" for ::1 at 2017-01-10 11:36:30 -0800
Processing by ReviewsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ld5yOdf4fbzQvyPdckfAtnjWqbDQQHkEZuszSrdlsjVfRYntGZymxwP80DI xe4O8Pe5X7cl7P2ioCR1bQ//waw==", "review"=>{"comment"=>"I like DSOTM"}, "commit"=>"Create Review", "album_id"=>"6"}
Album Load (0.4ms) SELECT "albums".* FROM "albums" WHERE "albums"."id" = ? LIMIT ? [["id", 6], ["LIMIT", 1]]
(0.2ms) begin transaction
(0.2ms) rollback transaction
Rendering reviews/new.html.erb within layouts/application
Rendered reviews/_form.html.erb (6.8ms)
Rendered reviews/new.html.erb within layouts/application (17.8ms)
Completed 200 OK in 251ms (Views: 222.5ms | ActiveRecord: 0.7ms)
不确定发生了什么。任何帮助,将不胜感激。这是我的路线:
Rails.application.routes.draw do
devise_for :users
resources :albums do
resources :reviews
end
root 'albums#index'
end
以下是我试图构建的评论表:
<%= simple_form_for([@album, @album.reviews.build]) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :comment %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
这是评论控制器(尚未完成此操作。基本上是编辑过的脚手架):
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :edit, :update, :destroy]
before_action :set_album
def index
@reviews = Review.all
end
def show
end
def new
@review = Review.new
end
def edit
end
def create
@review = Review.new(review_params)
respond_to do |format|
if @review.save
format.html { redirect_to @review, 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
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
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_album
@album = Album.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def review_params
params.require(:review).permit(:comment)
end
end
评论模型:
class Review < ApplicationRecord
belongs_to :user
belongs_to :album
end
专辑模型:
class Album < ApplicationRecord
has_many :reviews
belongs_to :user
end
答案 0 :(得分:1)
验证错误阻止了模型的持久化。您没有看到该错误,因为您在此行中创建了一个新实例:
simple_form_for [@album, @album.reviews.build]
您应该在控制器中预先构建审核,并将该行替换为:
simple_form_for [@album, @review]
然后你应该看到验证错误。如果仍然没有显示,或者您的某个属性被删除,可能是因为您的参数白名单将其排除在外。您可以通过在Rails控制台中构建和验证对象,然后检查模型实例上的错误来获取更多信息。