Ruby on Rails - 为产品添加评论

时间:2015-07-12 17:44:36

标签: ruby-on-rails ruby

我正在Rails创建我的第一个应用程序,一个电子商务,并希望将评论与产品相关联,我创建了所有必要的结构,我有用户模型,产品和评论,我想我做了一切是的,但是当我去渲染表单以插入评论时,我收到了这个错误:

表单中的第一个参数不能包含nil或为空

      else
      object      = record.is_a?(Array) ? record.last : record
      raise ArgumentError, "First argument in form cannot contain nil or be empty" unless object
      object_name = options[:as] || model_name_from_record_or_class(object).param_key
      apply_form_for_options!(record, object, options)
    end

呈现表单的视图是

<%= form_for(@comment, html: {multipart: true}) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
  <%= f.text_area :content, placeholder: "Compose new comment..." %>
</div>
<%= f.submit "Post", class: "btn btn-primary" %>
<span class="picture">
  <%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
</span>
<% end %>
<script type="text/javascript">
$('#comment_picture').bind('change', function() {
    size_in_megabytes = this.files[0].size/1024/1024;
    if (size_in_megabytes > 8) {
        alert('Maximum file size is 8MB. Please choose a smaller file.');
    }
});

评论控制器

class CommentsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy, :show]
before_action :correct_user,   only: :destroy

def show
  @comment = Comment.find(params[:id])
end

def create
  @comment = current_user.comment.build(comment_params)
  if @comment.save
    flash[:success] = "Comment created!"
    redirect_to store_url
  else
    @feed_items = []
    render 'static_pages/home'
  end
end

def destroy
  @comment.destroy
  flash[:success] = "Comment deleted"
  redirect_to request.referrer || store_url
end


private
 def comment_params
   params.require(:comment).permit(:content, :picture)
 end
 def correct_user
   @comment = current_user.comments.find_by(id: params[:id])
   redirect_to store_url if @comment.nil?
 end
end

展示产品控制器

class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]

# GET /products
# GET /products.json
def index
  @products = Product.paginate(page: params[:page])
end

# GET /products/1
# GET /products/1.json
def show
  @product = Product.find(params[:id])
  @comments = current_user.comments.build if logged_in?
  @comments = @product.comments.paginate(page: params[:page])

 @rating = Rating.where(product_id: @product.id, user_id:    @current_user.id).first
 unless @rating
   @rating = Rating.create(product_id: @product.id, user_id: @current_user.id, score: 0)
  end
 end

用户控制器

class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy

def index
  @users = User.where(activated: true).paginate(page: params[:page])
end

def show
  @user = User.find(params[:id])
  @comments = @user.comments.paginate(page: params[:page])
end

def new
  @user = User.new
end

def create
  @user = User.new(user_params)
  if @user.save
    @user.send_activation_email
    flash[:info] = "Please check your email to activate your account."
    redirect_to store_url
  else
    render 'new'
  end
end

def update
  if @user.update_attributes(user_params)
    flash[:success] = "Profile updated"
    redirect_to @user
  else
    render 'edit'
  end
end

def edit
  @user = User.find(params[:id])
end

def destroy
  User.find(params[:id]).destroy
  flash[:success] = "User deleted"
  redirect_to users_url
end

private

def user_params
  params.require(:user).permit(:name, :email, :password,
                             :password_confirmation)
end

# Confirms the correct user.
def correct_user
  @user = User.find(params[:id])
  redirect_to(store_url) unless current_user?(@user)
end
# Confirms an admin user.
def admin_user
  redirect_to(store_url) unless current_user.admin?
 end
end

如果有必要的话,我可以发布你也相信模特,感谢所有帮助我的人。

日志文件:

Started GET "/en/products/4" for 127.0.0.1 at 2015-07-12 20:30:01 +0200
Processing by ProductsController#show as HTML
 Parameters: {"locale"=>"en", "id"=>"4"}
Product Load (0.2ms)  SELECT  "products".* FROM "products" WHERE   "products"."id" = ? LIMIT 1  [["id", 4]]
 CACHE (0.0ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1  [["id", "4"]]
 User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
 Rating Load (0.2ms)  SELECT  "ratings".* FROM "ratings" WHERE "ratings"."product_id" = ? AND "ratings"."user_id" = ?  ORDER BY "ratings"."id" ASC LIMIT 1  [["product_id", 4], ["user_id", 1]]
  (0.2ms)  SELECT COUNT(*) FROM "ratings" WHERE "ratings"."product_id" = ?  [["product_id", 4]]
  (0.2ms)  SELECT COUNT(*) FROM "comments" WHERE "comments"."user_id" = ?  [["user_id", 1]]
 Rendered shared/_user_info.html.erb (3.0ms)
 Rendered shared/_error_messages.html.erb (0.1ms)
   (0.2ms)  SELECT SUM("ratings"."score") FROM "ratings" WHERE "ratings"."product_id" = ?  [["product_id", 4]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "ratings" WHERE "ratings"."product_id" = ?  [["product_id", 4]]
 Rendered shared/_comment_form.html.erb (7.6ms)
 CACHE (0.0ms)  SELECT SUM("ratings"."score") FROM "ratings" WHERE "ratings"."product_id" = ?  [["product_id", 4]]
 CACHE (0.0ms)  SELECT COUNT(*) FROM "ratings" WHERE "ratings"."product_id" = ?  [["product_id", 4]]
 Rendered products/show.html.erb within layouts/application (24.3ms)
 Rendered layouts/_header.html.erb (5.9ms)
 Rendered layouts/_footer.html.erb (1.1ms)
Completed 200 OK in 205ms (Views: 197.7ms | ActiveRecord: 1.1ms)


  Started POST "/comments?locale=en" for 127.0.0.1 at 2015-07-12 20:30:11 +0200
  Processing by StaticPagesController#home as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"1sIh3jNERiiitmbyHQaQpAomMIgjXKkBVA/RqsIYDhYkD73gFOeMKyYYtmjSr9mu0ngyKSephYmfJhvvlKMigg==", "comment"=>{"content"=>"Hello! "}, "commit"=>"Post", "locale"=>"comments"}
 comments translation not available
    Rendered static_pages/home.html.erb within layouts/application (70.2ms)
    User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
   Rendered layouts/_header.html.erb (4.4ms)
   Rendered layouts/_footer.html.erb (0.6ms)
   Completed 200 OK in 249ms (Views: 246.7ms | ActiveRecord: 0.2ms)

0 个答案:

没有答案