現在、rails で商品の評価機能を付けているのですが、星の画像がうまく表示されません。星の画像をassetsの直下に置いた場合以下のようなエラーが発生してしまいます。
Sprockets::Rails::Helper::AssetNotFound in Reviews#create
The asset "star-on.png" is not present in the asset pipeline.
しかし、assets/imagesに置いた場合、エラーは発生しないのですが、画像の表示がされません。原因としては
1、assets pipelineが上手く繋げられていなく、precompileが的確に設定できていないこと。
2、star-on.png等の画像が入っていないこと。
3、jsとの接続ができていないこと。
大まかにこの三点が現在考えられる原因です。ここ三日間程調べながら解決方法を考えていたのですが、解決できずにいるので質問させて頂きました。
コードも以下に貼っておくので、この原因がわかる方がいましたら是非お力を貸していただきたいです。
reviews_controller.rb
class ReviewsController < ApplicationController before_action :require_user_logged_in def index end def show end def create @user = current_user @review = Review.new @product = Product.where(params[:product_id]) @review = @user.reviews end end
create.html.erb
<%= form_for :reviews,url: {action: :create} do |f| %> <%= f.hidden_field :product_id, { value: @product.ids} %> <div class="form-group row"> <%= f.label :title, 'タイトル ', class:'col-md-3 col-form-label' %> <div class="col-md-9"> <%= f.text_field :title, class: "form-control" %> </div> </div> <div class="form-group row" id="star"> <%= f.label :rate,'評価 ', class:'col-md-3 col-form-label' %> <%= f.hidden_field :rate, id: :review_star %> </div> <script> $('#star').raty({ size : 36, starOff: '<%= asset_path('star-off.png') %>', starOn : '<%= asset_path('star-on.png') %>', starHalf: '<%= asset_path('star-half.png') %>', scoreName: 'post[rate]', half: true, }); </script> <div class="form-group row"> <%= f.label :content, '口コミ内容 ', class:'col-md-3 col-form-label' %> <div class="col-md-9"> <%= f.text_area :content, class: "form-control", rows: "8", placeholder:'口コミ内容がなくても、タイトルと評価のみで投稿できます。 まずは投稿してみましょう!投稿してから編集もできますよ!' %> </div> </div> <div class="form-group row justify-content-end"> <div class="col-md-9"> <%= f.submit '投稿する', class:"btn btn-success" %> </div> </div> <% end %>
asset.rb
Rails.application.config.assets.version = '1.0' Rails.application.config.assets.paths << Rails.root.join('node_modules') Rails.application.config.assets.precompile += %w( *.js )
production.rb
config.assets.compile = true
お手数お掛けしますが、よろしくお願いします。