1

我被困在一个问题上,我很想帮忙!我从 railscast #154 得到了多态关联

到目前为止,我已经让评论模型工作得很好。但是我也想创建一个评级模型,然后我可以添加 js 来制作漂亮。

查看了我能找到的所有其他教程/stackoverflows。它们都没有展示如何在控制器中使用多个多态关联。

我试图复制 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
4

0 回答 0