使用collect(&:foo).uniq的未定义方法

时间:2014-09-16 00:33:40

标签: ruby-on-rails ruby methods undefined

我正在尝试显示一个书签网址集合,按当前用户的主题名称进行分类。我是编程新手,我需要帮助解决此错误,@topics = @bookmarks.collect(&:topic).uniq导致错误:

undefined method `topic' for #<Bookmark:0xacfa42c>
Extracted source (around line #4):
def index
    @bookmarks = current_user.bookmarks
    @topics = @bookmarks.collect(&:topic).uniq
    @liked_bookmarks = current_user.likes.collect(&:bookmark)
    @liked_topics = @liked_bookmarks.collect(&:topic).uniq
  end

app/controllers/user_bookmarks_controller.rb:4:in `index'

这是我的主题控制器:

class TopicsController < ApplicationController
  before_action :set_topic, only: [:show, :edit, :update, :destroy]

  def index
    @topics = Topic.all
  end

  def show
    @bookmarks = @topic.bookmarks
  end

  def new
    @topic = Topic.new
  end

  def edit
  end

  def create
    @topic = Topic.new(topic_params)

    respond_to do |format|
      if @topic.save
        format.html { redirect_to @topic, notice: 'Topic was successfully created.' }
        format.json { render action: 'show', status: :created, location: @topic }
      else
        format.html { render action: 'new' }
        format.json { render json: @topic.errors, status: :unprocessable_entity }
      end
    end
  end

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

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

  private
    def set_topic
      @topic = Topic.find(params[:id])
    end

    def topic_params
      params.require(:topic).permit(:name)
    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

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

根据您提供的信息,您的Bookmark模型可能有很多topics。给定的Bookmark将有一个名为topics但不是topic的方法。你可能想尝试类似的东西:

@topics = @bookmarks.topics.uniq