簡體   English   中英

缺少模板事件/favorite.js.erb

[英]Missing template events/favorite.js.erb

我正在嘗試讓 gem 充當 votable 工作而無需刷新頁面。

在沒有 AJAX 的情況下,我確實有作為 votable gem 使用頁面刷新的行為。

我正在按照 Superails 在此博客上的步驟進行操作。

我覺得他的設置和我的設置之間的主要區別在於我有一個部分嵌套在一個部分中。

我的index.html.erb用於我的事件 model,我正在渲染我的第一個部分:

 <div class="event-list-wrapper">
    <% @events.upcoming_events.each do |event| %>
      <%= render 'event', event: event %>
    <% end %>
  </div> 

我有我的初始_event.html.erb部分:

<div class="card shadow rounded mb-3">
  <div class="top-card">
    <div class="card-image">
        <% if event.event_image.attached? %>
      <%= link_to image_tag(event.event_image, class: "card-img-top event-index-image").html_safe, event %>
     <% else %>
      <img src=<%= "https://dancewise.s3.amazonaws.com/Blank+Event+Image.png" %> class=" card-img-top even-size-event">
     <% end %>
    </div>
    <div class="heart-container">
         <%= render "events/favorite-link", event: event %>
    </div>
  </div>

然后是我的_favorite-link.html.erb部分:

<%= content_tag "div", id: "upvote-#{event.id}" do %>
  <%= link_to upvote_event_path(event), method: :get, remote: true do %>
     <% if current_user.voted_up_on? event %>
            <%= link_to upvote_event_path(event), method: :patch do %>
               <svg 
                 xmlns="http://www.w3.org/2000/svg" 
                 viewBox="0 0 24 24" 
                 fill="#ff2929" 
                 stroke-width="1.2"
                 stroke="#f2f2f2" 
                 class="w-6 h-6">
               <path d="M11.645 20.91l-.007-.003-.022-.012a15.247 15.247 0 01-.383-.218 25.18 25.18 0 01-4.244-3.17C4.688 15.36 2.25 12.174 2.25 8.25 2.25 5.322 4.714 3 7.688 3A5.5 5.5 0 0112 5.052 5.5 5.5 0 0116.313 3c2.973 0 5.437 2.322 5.437 5.25 0 3.925-2.438 7.111-4.739 9.256a25.175 25.175 0 01-4.244 3.17 15.247 15.247 0 01-.383.219l-.022.012-.007.004-.003.001a.752.752 0 01-.704 0l-.003-.001z" />
               </svg>
            <% end %>
        <% else %>
           <%= link_to upvote_event_path(event), method: :patch do %>
               <svg 
                 xmlns="http://www.w3.org/2000/svg" 
                 fill="#000000" 
                 fill-opacity="0.5" 
                 viewBox="0 0 24 24"
                 data-user-logged-in="<%= user_signed_in? %>"
                 stroke-width="1.2" 
                 stroke="#f2f2f2" 
                 class="w-6 h-6">
               <path stroke-linecap="round" stroke-linejoin="round" d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12z" />
               </svg>
            <% end %>
      <% end %>
<% end %>
<% end %>

我也有我favorite.js.erb的 .js.erb 文件:

document.getElementById("upvote-<%= @event.id %>").innerHTML = "<%= j render "events/favorite-link", event: event %>";

還有我的events_controller.rb

def upvote
    if current_user.voted_up_on? @event
       @event.unvote_by current_user
    else
      @event.upvote_by current_user
    end
     render "favorite.js.erb"
  end

我嘗試使用 events_controller 中的路徑,甚至嘗試將 favorite.js.erb 復制到指示錯誤的不同路徑,但仍然遇到相同的錯誤。

/Users/ogarocious/Desktop/RubyWorld/wherecanwedance-dw/app/views/favorite.js.erb /Users/ogarocious/Desktop/RubyWorld/wherecanwedance-dw/app/views/events/favorite.js.erb

01:58:49 web.1  | Completed 500 Internal Server Error in 24ms (ActiveRecord: 4.5ms | Allocations: 10048)
01:58:49 web.1  | 
01:58:49 web.1  | 
01:58:49 web.1  |   
01:58:49 web.1  | ActionView::MissingTemplate (Missing template events/favorite.js.erb, application/favorite.js.erb with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}.
01:58:49 web.1  | 
01:58:49 web.1  | Searched in:
01:58:49 web.1  |   * "/Users/ogarocious/Desktop/RubyWorld/wherecanwedance-dw/app/views"
01:58:49 web.1  |   * "/Users/ogarocious/.rvm/gems/ruby-3.0.2/gems/devise-4.8.1/app/views"
01:58:49 web.1  |   * "/Users/ogarocious/.rvm/gems/ruby-3.0.2/gems/actiontext-7.0.4/app/views"
01:58:49 web.1  |   * "/Users/ogarocious/.rvm/gems/ruby-3.0.2/gems/actionmailbox-7.0.4/app/views"
01:58:49 web.1  | 
01:58:49 web.1  |              
01:58:49 web.1  |   
01:58:49 web.1  | app/controllers/events_controller.rb:19:in `upvote'

我的routes.rb

  resources :events do
    member do
      get "upvote", to: "events#upvote"
    end

我不確定我還能遺漏什么,但我覺得部分中的部分導致了問題,我需要以某種方式修改事件 controller 中 favorite.js.erb 文件的路徑,任何見解都值得贊賞!

在 Rails 7 中,我們不再使用.js.erb 我們改用熱線。 這是使用 hotwires 的format.turbo_stream添加喜歡/收藏夾的更新指南: https://blog.corsego.com/acts-as-votable-4-hotwire

您還可以在這里找到增加 like_count 的簡單指南: like_count ://blog.corsego.com/hotwire-turbo-button-to-like-record

我希望它有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM