我想要完成的是在我的视图中创建POST和DELETE链接,例如,像一个类似的按钮。
我在我的用户模型(user.rb)中创建了一个方法(def喜欢),该方法将检查用户是否喜欢书签,并显示'不像'或者'喜欢'按钮,分别基于我喜欢的方法的响应。
我有以下代码:
routes.rb中:
Rails.application.routes.draw do
resources :topics do
resources :bookmarks do
resources :likes, only: [:create, :destroy]
end
end
devise_for :users
get 'welcome/index'
get 'welcome/about'
root to: 'welcome#index'
post :incoming, to: 'incoming#create'
end
User.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :topics, dependent: :destroy
has_many :bookmarks, dependent: :destroy
has_many :likes, dependent: :destroy
def admin?
role == 'admin'
end
def moderator?
role == 'moderator'
end
def liked(bookmark)
likes.where(bookmark_id: bookmark.id).first
end
end
likes_controller.rb:
class LikesController < ApplicationController
def create
@bookmark = Bookmark.find(params[:bookmark_id])
like = current_user.likes.build(bookmark: @bookmark)
if like.save
flash[:notice] = "Bookmark was Liked!"
redirect_to [@bookmark.topic, @bookmark]
else
flash[:error] = "Unable to Like Bookmark"
redirect_to @bookmark
end
end
def destroy
@bookmark = Bookmark.find(params[:bookmark_id])
like = current_user.likes.find(params[:id])
if like.destroy
flash[:notice] = "Bookmark was Un-liked."
redirect_to [@bookmark.topic, @bookmark]
else
flash[:error] = "Error Un-liking bookmark."
redirect_to [@bookmark.topic, @bookmark]
end
end
end
最后我喜欢偏爱,_like.html.erb:
<div>
<% if like = current_user.liked(bookmark) %>
<%= link_to [bookmark, like], class: 'btn btn-danger', method: :delete do %>
<i class="glyphicon glyphicon-star-empty"> </i> Unlike
<% end %>
<% else %>
<%= link_to [bookmark, Like.new], class: 'btn btn-primary', method: :post do %>
<i class="glyphicon glyphicon-star"> </i> Like
<% end %>
<% end %>
</div>
我想在我的主题中使用#index view:
<h2 id="page-title">All Bookmarks</h2>
<div class="row">
<div class="col-md-8">
<% @topics.each do |topic| %>
<h4><%= link_to "##{topic.title}", topic_path(topic.id) %></br></h4>
<% topic.bookmarks.each do |bookmark| %>
<%= render partial: 'likes/like', locals: { bookmark: bookmark } %>
<%= link_to "#{bookmark.url}", "http://#{bookmark.url}", target: "_blank" %></br>
<% end %>
<% end %>
</div>
<div class="col-md-4">
<%= link_to "New Topic", new_topic_path, class: 'btn btn-primary' %>
</div>
</div>
当我运行服务器并转到索引视图时,收到以下NoMethodError:
未定义的方法`bookmark_likes_path&#39;对于#&lt;#:0x007ffe2817b5a0&gt;
指向这一行:
<%= link_to [bookmark, Like.new], class: 'btn btn-primary', method: :post do %>
哪个位于我喜欢的部分(_like.html.erb)
我认为问题是路由问题,因为据我所知,问题是说bookmark_likes_path方法是nil,如果我检查我的rake路由,我会得到以下内容:(使用grep)
topic_bookmark_likes POST /topics/:topic_id/bookmarks/:bookmark_id/likes(.:format) likes#create
topic_bookmark_like DELETE /topics/:topic_id/bookmarks/:bookmark_id/likes/:id(.:format) likes#destroy
这表示我喜欢的路由,它表示(因为是嵌套资源)访问POST和DELETE操作的正确路由,这些操作位于主题内,因此路径需要是/ topics /:topic_id / bookmarks / :bookmark_id /喜欢用于创建,和/ topics /:topic_id / bookmarks /:bookmark_id / likes /:id用于删除,所以我更新了喜欢这样的部分:
<div>
<% if like = current_user.liked(bookmark) %>
<%= link_to [@topic, bookmark, like], class: 'btn btn-danger', method: :delete do %>
<i class="glyphicon glyphicon-star-empty"> </i> Unlike
<% end %>
<% else %>
<%= link_to [@topic, bookmark, Like.new], class: 'btn btn-primary', method: :post do %>
<i class="glyphicon glyphicon-star"> </i> Like
<% end %>
<% end %>
</div>
仍然是同样的错误。我已经尝试长时间调试此错误,但我无法修复它,我需要一些帮助。
提前致谢!
答案 0 :(得分:0)
我已经修好了。
问题在于嵌套资源确实,我所做的就是将资源分开:
resources :topics do
resources :bookmarks
end
resources :bookmarks do
resources :likes, only: [:create, :destroy]
end
这解决了这个问题,所以我不必为了深入了解细节而烦恼。