我遇到了问题,我会爱上一只手!我从railscast#154获得了多态关联
到目前为止,我已经得到了评论模型的精彩工作。但是我想创建一个评级模型,然后我可以添加js来制作漂亮的。
查看我能找到的每个其他教程/ stackoverflow。它们都没有显示如何在控制器中使用多个多态关联。
我试图复制article_controller的代码,但我不知道如何为这两个模型做到这一点。到目前为止,我的尝试都没有成功。部分表单具有正确的名称,它是_ratings.html.erb
我的错误:
Missing partial ratings/ratings with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* "/code/jimbo/bigdog/app/views"
* "/home/user/.rvm/gems/ruby-2.0.0-p247@lulla-core/gems/kaminari-0.14.1/app/views"
* "/home/user/.rvm/gems/ruby-2.0.0-p247@lulla-core/gems/devise-3.1.1/app/views"
我的模特:
class Rating
include Mongoid::Document
include Mongoid::MultiParameterAttributes
field :value, type: Integer, default: "0"
belongs_to :rateable, polymorphic: true
end
我的控制器:
class RatingsController < ApplicationController
before_filter :load_rateable
def index
@ratings = @rateable.rating
end
def show
@rating = @rateable.ratings.find(params[:id])
respond_to do |format|
format.html # show.html.erb
end
end
def new
@rating = @rateable.rating.new
end
def create
@rating = @rateable.rating.new(params[:rating])
if @rating.save
redirect_to @rateable, notice: "Rating created."
else
render :new
end
end
private
def load_rateable
resource, id = request.path.split('/')[1, 2]
@rateable = resource.singularize.classify.constantize.find(id)
end
# def load_commentable
# klass = [Article, Photo, Event].detect { |c| params["#{c.name.underscore}_id"] }
# @rateable = klass.find(params["#{klass.name.underscore}_id"])
# end
end
文章控制器
class ArticlesController < ApplicationController
# GET /articles
# GET /articles.json
def index
@articles = Article.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @articles }
end
end
# GET /articles/1
# GET /articles/1.json
def show
@article = Article.find(params[:id])
@commentable = @article
@comments = @commentable.comments
@comment = Comment.new
respond_to do |format|
format.html # show.html.erb
format.json { render json: @article }
end
end
# GET /articles/new
# GET /articles/new.json
def new
@article = Article.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @article }
end
end
# GET /articles/1/edit
def edit
@article = Article.find(params[:id])
end
# POST /articles
# POST /articles.json
def create
@article = Article.new(params[:article])
respond_to do |format|
if @article.save
format.html { redirect_to @article, notice: 'Article was successfully created.' }
format.json { render json: @article, status: :created, location: @article }
else
format.html { render action: "new" }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end
# PUT /articles/1
# PUT /articles/1.json
def update
@article = Article.find(params[:id])
respond_to do |format|
if @article.update_attributes(params[:article])
format.html { redirect_to @article, notice: 'Article was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end
# DELETE /articles/1
# DELETE /articles/1.json
def destroy
@article = Article.find(params[:id])
@article.destroy
respond_to do |format|
format.html { redirect_to articles_url }
format.json { head :no_content }
end
end
end
我的观点:
show.html(文章)
<%= render "comments/comments" %>
<%= render "comments/form" %>
<%= render "ratings/ratings" %>
<%= render "ratings/form" %>
路线:
resources :articles do
resources :comments
resources :ratings
end