安装ratyrate之后我就像在GItHub的指令那样完成了 我收到了这个错误 我需要在控制器上添加一些东西吗?或者是什么? 我希望你能帮助我解决这个问题
我需要在控制器上添加一些东西吗?或者是什么? 我希望你能帮助我解决问题
我的模型user.rb(使用设计)
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
ratyrate_rater
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :recipe
end
我的模型recipe.rb
class Recipe < ApplicationRecord
belongs_to :user
has_many :ingredients
has_many :directions
has_many :comments, as: :commentable
ratyrate_rateable "recipe_rating"
accepts_nested_attributes_for :ingredients,
reject_if: proc { |attributes| attributes['name'].blank? },
allow_destroy: true
accepts_nested_attributes_for :directions,
reject_if: proc { |attributes| attributes['step'].blank? },
allow_destroy: true
validates :title, :description, :image, presence: true
has_attached_file :image, styles: { medium: "400x400#"}
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
我的index.html.haml
- @recipe.each_slice(3) do |recipes|
.row
-recipes.each do |recipe|
.col-md-4
.recipe
.image_wrapper
= link_to recipe do
=image_tag recipe.image.url(:medium)
%h2= link_to recipe.title,recipe
.row
.small-2.large-2.columns
= imdb_style_rating_for @recipe, current_user
%br/
.small-2.large-4.columns
- if current_user
Recipe Ratings: #{rating_for @recipe, "recipe_rating"}
我的ricipes_controller
class RecipesController < ApplicationController
before_action :find_recipe, only: [:show,:edit, :update, :destroy]
before_action :authenticate_user!, exept: [:index, :show]
def index
@recipe = Recipe.all.order("created_at DESC")
end
def show
end
def new
@recipe = current_user.recipe.build
end
def create
@recipe = current_user.recipe.build(recipe_params)
if @recipe.save
redirect_to @recipe, notice: "Successfully created new recipe"
else
render 'new'
end
end
def edit
end
def update
if @recipe.update(recipe_params)
redirect_to @recipe
else
render 'edit'
end
end
def destroy
@recipe.destroy
redirect_to root_path, notice: "Successfully deleted recipe"
end
private
def recipe_params
params.require(:recipe).permit(:title, :description, :image, ingredients_attributes: [:id, :name, :_destroy], directions_attributes: [:id, :step, :_destroy])
end
def find_recipe
@recipe = Recipe.find(params[:id])
end
end