接收ActionView :: MissingTemplate(缺少模板用户/关注,应用程序/遵循AJAX请求,Rails 5

时间:2018-02-25 05:56:22

标签: ruby-on-rails ruby ajax

按下follow / unfollow切换按钮后,我收到_follow.html.erb的ActionView :: MissingTemplate异常。我似乎无法通过follow / unfollow按钮超越这一点。我尝试了6种不同的方法,似乎都没有为我呈现正确的结果。如何设置follow / unfollow切换按钮在我的索引/显示视图中正常工作?

服务器开发日志

Started GET "/u/jimmydean/follow" for 127.0.0.1 at 2018-02-25 00:43:41 -0500
Processing by UsersController#follow as JS
  Parameters: {"id"=>"jimmydean"}
  User Load (3.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."slug" = $1 LIMIT $2  [["slug", "jimmydean"], ["LIMIT", 1]]
   (0.0ms)  BEGIN
  SQL (1.0ms)  INSERT INTO "notifications" ("user_id", "actor_id", "notify_type", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["user_id", 1], ["actor_id", 2], ["notify_type", "follow"], ["created_at", "2018-02-25 05:43:42.022447"], ["updated_at", "2018-02-25 05:43:42.022447"]]
   (0.0ms)  COMMIT
Completed 500 Internal Server Error in 1039ms (ActiveRecord: 5.0ms)



ActionView::MissingTemplate (Missing template users/follow, application/follow with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :slim, :coffee, :jbuilder]}. Searched in:

index.html.erb

<div class="col-md-4">
      <div class="others-like-me-box">
        <div class="others-like-me-box-header user">
          <%= render partial: 'users/shared/follow', locals: {users: @users} %>
        </div>
      </div>
    </div>

_follow.html.erb

<ul class="list-unstyled">
  <% @users.each do |user| %>
    <li>
      <div class="media">
        <%= user_avatar_dashboard(user) %>
        <div class="media-body">
          <h6 class="mt-0 mb-1"><%= fa_icon 'user-o' %><strong> <%= link_to user.username, user %></strong></h6>

          <div class="cool-ppl-to-follow-btn-pos">
            <% if current_user.id != user.id %>
            <% if user.followed_by?(current_user) %>
             <%= link_to 'Unfollow', unfollow_user_path(user), class: 'btn btn-outline-danger', remote: true %>
             <% else %>
             <%= link_to 'Follow', follow_user_path(user), class: 'btn btn-outline-success', remote: true %>
            <% end %>
            <% end %>
          </div>
        </div>
      </div>
    </li>
  <% end %>
</ul>

users_controller.rb

 def follow
    @user = User.friendly.find(params[:id])
    if current_user
       current_user.follow(@user)
    end
    respond_to do |format|
        format.js 
    end
  end

  def unfollow
    @user = User.friendly.find(params[:id])
    if current_user
       current_user.stop_following(@user)
    end
    respond_to do |format|
      format.js
    end
  end

  def block
    @user = User.friendly.find(params[:id])
    if current_user
      current_user.block(@user)
    end
    respond_to do |format|
      format.js
    end
  end

  def unblock
    @user = User.friendly.find(params[:id])
    if current_user
      current_user.unblock(@user)
    end
    respond_to do |format|
      format.js
    end
  end

的routes.rb

resources :users, only: [:show, :index, :update], path: 'u' do
    get 'users/:username' => 'users#show'
    patch 'users/:username', to: 'users#update'
    member do
      get :follow
      get :unfollow
      get :block
      get :unblock
    end
  end

follow.js.erb

<% if @user.followed_by?(current_user) %>
$("#user-<%= @user.id %>").html("<%= link_to 'Unfollow', unfollow_user_path(@user), remote: true %>")
<% else %>
$("#user-<%= @user.id %>").html("<%= link_to 'Follow', follow_user_path(@user), remote: true %>")
<% end %>

更新

当我点击关注按钮时,状态不会改变,用户也不会真正关注目标用户。我的服务器日志显示如下。

Started GET "/u/jimmydean/follow" for 127.0.0.1 at 2018-02-25 12:01:30 -0500
Processing by UsersController#follow as JS
  Parameters: {"id"=>"jimmydean"}
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."slug" = $1 LIMIT $2  [["slug", "jimmydean"], ["LIMIT", 1]]
   (0.0ms)  BEGIN
  SQL (75.1ms)  INSERT INTO "notifications" ("user_id", "actor_id", "notify_type", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["user_id", 1], ["actor_id", 2], ["notify_type", "follow"], ["created_at", "2018-02-25 17:01:30.780542"], ["updated_at", "2018-02-25 17:01:30.780542"]]
   (151.1ms)  COMMIT
  Rendering users/follow.js.erb
DEPRECATION WARNING: Setting custom parent classes is deprecated and will be removed in future versions. (called from parent_class_name at C:/Ruby24-x64/lib/ruby/gems/2.4.0/bundler/gems/acts_as_follower-c5ac7b9601c4/lib/acts_as_follower/follower_lib.rb:10)
  Follow Load (0.0ms)  SELECT  "follows".* FROM "follows" WHERE "follows"."followable_id" = $1 AND "follows"."followable_type" = $2 AND "follows"."blocked" = $3 AND "follows"."follower_id" = $4 AND "follows"."follower_type" = $5 ORDER BY "follows"."id" ASC LIMIT $6  [["followable_id", 1], ["followable_type", "User"], ["blocked", "f"], ["follower_id", 2], ["follower_type", "User"], ["LIMIT", 1]]
  Rendered users/follow.js.erb (4.0ms)
Completed 200 OK in 1399ms (Views: 1102.6ms | ActiveRecord: 228.2ms)

1 个答案:

答案 0 :(得分:0)

要让C:\Users\Username\AppData\Local\lxss\rootfs 自动找到不显眼的JS部分,它也应该放在UsersController#follow文件夹中(即users)。

您的代码看起来很好,但由于您的部分代码位于app/views/users/follow.js.erb,因此您可能不小心将JS部分放在users/shared/_follow.html.erb中。