0

我正在为我的 ruby​​ on rails 4.0 应用程序使用 jQuery 插件Raty,它似乎可以正常工作,只是星星的图像没有加载到屏幕上。

在此处输入图像描述

所以这些图像应该是星星^当我在控制台中将鼠标悬停在运行网络砖时,会输出以下内容。(我的脚手架叫做reviews)

在 2014-03-28 14:15:42 -0400 开始 GET "/reviews/star-off.png" for 127.0.0.1 由 ReviewController#show as PNG 参数处理:{"id"=>"star-off"}审查负载 (1.3ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."id" = $1 LIMIT 1 [["id", "star-off"]] 完成 404 Not Found in 2ms ActiveRecord:: RecordNotFound(找不到 id=star-off 的评论):app/controllers/reviews_controller.rb:67:in `set_review'

我目前在 apps/assets/javascripts/images 中有星形图像,但也尝试将它们放在 app/assets/images 和 app/views/reviews 中,但它们仍然不会出现。我的问题是它们不在正确的目录中(如果是,它们应该在哪个目录中)还是我需要手动将一些代码添加到我的评论控制器中?谢谢。

编辑:所以当我尝试在我的索引页面中使用它时,我只会收到一条错误消息,所以我必须对我的 routes.rb 文件做些什么?

ActionController::RoutingError (NNo route matches [GET] "/star-off.png")

编辑:这里要求的是 routes.rb

ConcertReview::Application.routes.draw do
resources :reviews

get "review/index"
get "review/artist"
get "review/date"
get "review/venue"
get "review/genre"
get "review/comments"

root 'reviews#index'

get 'reviews/' => 'reviews#index'
end

这是reviews_controller.rb(从脚手架自动生成并且没有修改)

class ReviewsController < ApplicationController
  before_action :set_review, only: [:show, :edit, :update, :destroy]

  # GET /reviews
  # GET /reviews.json
  def index
    @reviews = Review.all
  end

  # GET /reviews/1
  # GET /reviews/1.json
  def show
  end

  # GET /reviews/new
  def new
    @review = Review.new
  end

  # GET /reviews/1/edit
  def edit
  end

  # POST /reviews
  # POST /reviews.json
  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 action: 'show', status: :created, location: @review }
      else
        format.html { render action: 'new' }
        format.json { render json: @review.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /reviews/1
  # PATCH/PUT /reviews/1.json
  def update
    respond_to do |format|
      if @review.update(review_params)
        format.html { redirect_to @review, notice: 'Review was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @review.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /reviews/1
  # DELETE /reviews/1.json
  def destroy
    @review.destroy
    respond_to do |format|
      format.html { redirect_to reviews_url }
      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

    # Never trust parameters from the scary internet, only allow the white list through.
    def review_params
      params.require(:review).permit(:artist, :venue, :date, :genre, :sound, :stagePresence, :songSelection, :overallRating, :comments)
    end
end
4

1 回答 1

0

我在我的项目中遇到了同样的问题。明星图像确实属于您尝试过的 app/assets/images 。但是,您需要传递一个路径选项,如下所示:

$('div').raty({
  readOnly: true,
  halfScore: true,
  score: 3, 
  path: '/assets'
});

这对我有用。

此外,如果您打开了 turbolinks,您将不得不对任何 $(document).ready(function () {...}); 进行 tweek 处理。你可能有。这就是我在这个问题之后遇到的问题。

于 2014-04-21T01:42:56.107 回答