无法找到没有ID的书签

时间:2014-09-17 20:23:19

标签: ruby-on-rails

我正在尝试在我的社交书签应用程序上构建一个功能,用户可以点击书签网址下的类似按钮将该书签存储在他们的"喜欢"数据库。这是我在浏览器中点击时出现的错误(错误在行@bookmark = Bookmark.find(params[:bookmark_id])处抛出:

Couldn't find Bookmark without an ID

class LikesController < ApplicationController
  def create
     @bookmark = Bookmark.find(params[:bookmark_id])
     like = current_user.likes.build(bookmark: @bookmark)

     authorize like

app/controllers/likes_controller.rb:3:in `create'

这是我喜欢的控制器:

class LikesController < ApplicationController
  def create
     @bookmark = Bookmark.find(params[:bookmark_id])
     like = current_user.likes.build(bookmark: @bookmark)

     authorize like

     if like.save
       flash[:notice] = "Liked bookmark"
       redirect_to [@bookmark.topic, @bookmark]
     else
       flash[:error] = "Unable to add like. Please try again."
      redirect_to [@bookmark.topic, @bookmark]
     end
  end
  def destroy
      @bookmark = Bookmark.find(params[:bookmark_id])
      like = current_user.likes.find(params[:id])

      authorize like

      if like.destroy
        flash[:notice] = "Removed like."
        redirect_to [@bookmark.topic, @bookmark]
      else
        flash[:error] = "Unable to remove like. Please try again."
        redirect_to [@bookmark.topic, @bookmark]
      end
    end
  end

这是我的书签控制器:

class BookmarksController < ApplicationController
  before_action :set_bookmark, only: [:show, :edit, :update, :destroy]

  def index
    @bookmarks = Bookmark.all
  end

  def show
  end

  def new
    @bookmark = Bookmark.new
  end

  def edit
  end

  def create
    bookmark = Bookmark.where(url: params[:bookmark][:url]).first

    @bookmark = bookmark.present? ? bookmark : Bookmark.new(bookmark_params)

    if @bookmark.save
      @bookmark.users << current_user
      Rails.logger.info ">>>>>>>>>>>>> Bookmark: #{@bookmark.inspect}"

      topic_names = params[:topic_names].split(' ')
      topic_names.each do |topic_name|
        name = topic_name.sub(/#/, '')

        @bookmark.topics << Topic.find_or_create_by_name(name)
      end
      respond_to do |format|
        format.html { redirect_to @bookmark, notice: 'Bookmark was successfully created.' }
        format.json { render action: 'show', status: :created, location: @bookmark }
      end
    else
      respond_to do |format|
        format.html { render action: 'new' }
        format.json { render json: @bookmark.errors, status: :unprocessable_entity }
      end
    end   
  end

  def update
    respond_to do |format|
      if @bookmark.update(bookmark_params)
        format.html { redirect_to @bookmark, notice: 'Bookmark was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @bookmark.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @bookmark.destroy
    respond_to do |format|
      format.html { redirect_to bookmarks_url }
      format.json { head :no_content }
    end
  end

  private
    def set_bookmark
      @bookmark = Bookmark.find(params[:id])
    end

    def bookmark_params
      params.require(:bookmark).permit(:url)
    end
end

这是我的传入(主题和书签)控制器

class BookmarksController < ApplicationController
  before_action :set_bookmark, only: [:show, :edit, :update, :destroy]

  def index
    @bookmarks = Bookmark.all
  end

  def show
  end

  def new
    @bookmark = Bookmark.new
  end

  def edit
  end

  def create
    bookmark = Bookmark.where(url: params[:bookmark][:url]).first

    @bookmark = bookmark.present? ? bookmark : Bookmark.new(bookmark_params)

    if @bookmark.save
      @bookmark.users << current_user
      Rails.logger.info ">>>>>>>>>>>>> Bookmark: #{@bookmark.inspect}"

      topic_names = params[:topic_names].split(' ')
      topic_names.each do |topic_name|
        name = topic_name.sub(/#/, '')

        @bookmark.topics << Topic.find_or_create_by_name(name)
      end
      respond_to do |format|
        format.html { redirect_to @bookmark, notice: 'Bookmark was successfully created.' }
        format.json { render action: 'show', status: :created, location: @bookmark }
      end
    else
      respond_to do |format|
        format.html { render action: 'new' }
        format.json { render json: @bookmark.errors, status: :unprocessable_entity }
      end
    end   
  end

  def update
    respond_to do |format|
      if @bookmark.update(bookmark_params)
        format.html { redirect_to @bookmark, notice: 'Bookmark was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @bookmark.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @bookmark.destroy
    respond_to do |format|
      format.html { redirect_to bookmarks_url }
      format.json { head :no_content }
    end
  end

  private
    def set_bookmark
      @bookmark = Bookmark.find(params[:id])
    end

    def bookmark_params
      params.require(:bookmark).permit(:url)
    end
end

这是我的用户书签控制器:

class UserBookmarksController < ApplicationController
  def index
    @bookmarks = current_user.bookmarks
    @liked_bookmarks = current_user.likes.collect(&:bookmark)
    @liked_topics = @liked_bookmarks.collect(&:topic).uniq
  end
end

您可以提供任何有助于我解决此错误的见解,我们将不胜感激。如果您需要查看任何其他文件,请告诉我。

编辑更新:添加请求的文件:

这是我的主题索引视图文件,我试图点击“赞”按钮:

<h1>All Bookmarks</h1>

    <ul class="topics">
        <% @topics.each do |topic| %>
          <li><%= link_to "##{topic.name}", topic %></li>
          <ul class="bookmarks">
            <%= render topic.bookmarks %>
          </ul>    
        <% end %>
    </ul>
    <br>

这是在上面的视图中呈现的部分:

 
<li><%= bookmark.url %></li>
 <div class="like">        
      <% if show_remove_button %>     
          <%= link_to "Remove", user_bookmark_path(get_user_bookmark_for(bookmark)), method: :delete %>
      <% else %>    
        <% if like = current_user.liked(bookmark) %>
          <%= link_to [bookmark, like], method: :delete do %><u>Unlike</u><% end %>
        <% else %>
          <%= link_to [Like.new], method: :post do %><u>Like</u><% end %>
       <% end %> 
    <% end %>
</div>

1 个答案:

答案 0 :(得分:2)

是的,你没有传递bookmark_id,请尝试:

<%= link_to [bookmark, Like.new], method: :post do %><u>Like</u><% end %>