用于rails应用程序图像的jQuery插件未加载

时间:2014-03-28 18:22:16

标签: javascript jquery ruby-on-rails ruby raty

我在我的ruby on rails 4.0应用程序中使用jQuery插件Raty,它似乎正在工作,除了星星的图像没有加载到屏幕上。

enter image description here

所以那些图像应该是星星^当我在运行web砖时在控制台中鼠标悬停它们时输出以下内容。 (我的脚手架叫做评论)

  

开始GET" /reviews/star-off.png"在2014-03-28 14:15:42 -0400为127.0.0.1   由ReviewsController处理#显示为PNG     参数:{" id" =>" star-off"}     查看加载(1.3ms)SELECT"评论"。* FROM"评论"在哪里"评论"。" id" = $ 1 LIMIT 1 [[" id"," star-off"]]   完成404未找到2ms     ActiveRecord :: RecordNotFound(找不到具有id = star-off的Review):     app / controllers / reviews_controller.rb:67:在`set_review'

我目前在apps / assets / javascripts / images中有明星图片,但也尝试将它们放在app / assets / images和app / views / reviews中,但它们仍然无法显示。我的问题是他们不在正确的目录中(如果是,他们应该在哪个目录中)或者我是否需要手动向我的评论控制器添加一些代码?谢谢。

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

  

ActionController :: RoutingError(NNo路由匹配[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

1 个答案:

答案 0 :(得分:0)

我的项目遇到了同样的问题。明星图像确实属于app / assets / images,就像您尝试过的那样。但是,您需要传递raty 路径选项,例如:

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

这对我有用。

此外,如果你有turbolinks,你将不得不调整任何$(文件).ready(function(){...});你可能有。这就是我在这之后遇到的问题。