Rails使用永久链接/令牌网址进行多态评论

时间:2014-07-21 01:04:35

标签: ruby-on-rails polymorphic-associations acts-as-commentable

在我的应用中,我有一个评论系统,主要基于此railscast。现在,在我的模型中,我将to_param更改为随机字符串,因此ID不在网址中。但那会打破评论。

status.rb

class Status < ActiveRecord::Base
    attr_accessible :content, :member_id, :document_attributes, :permalink
    belongs_to :member 
    belongs_to :document
    has_many :comments, as: :commentable, dependent: :destroy

    before_create :make_it_permalink

    accepts_nested_attributes_for :document

    def to_param
        permalink
    end

    private

    def make_it_permalink
        # this can create permalink with random 12 digit alphanumeric
        self.permalink = SecureRandom.hex(12)
    end

end

statuses_controller.rb

class StatusesController < ApplicationController

before_filter :authenticate_member!, only: [:index, :new, :create, :destroy] 
before_filter :find_member

rescue_from ActiveRecord::RecordNotFound do
    render file: 'public/404', status: 404, formats: [:html]
end

def index
    @statuses = Status.order('created_at desc').page(params[:page]).per_page(21)
    respond_to do |format|
      format.html # index.html.erb
      format.js
    end
end

def show
    @status = Status.find_by_permalink(params[:id])
    @commentable = @status
    @comments = @commentable.comments.order('created_at desc').page(params[:page]).per_page(15)
    @comment = @commentable.comments.new
    respond_to do |format|
      format.html # show.html.erb
      format.json { redirect_to profile_path(current_member) }
    end
end

def new
    @status = Status.new
    @status.build_document

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @status }
      format.js
    end
end

def create
    @status = current_member.statuses.new(params[:status])

    respond_to do |format|
      if @status.save
        @activity = current_member.create_activity(@status, 'created')
        format.html { redirect_to :back }
        format.json
        format.js
      else
        format.html { redirect_to profile_path(current_member), alert: 'Post wasn\'t created. Please try again and ensure image attchments are under 10Mbs.'  }
        format.json { render json: @status.errors, status: :unprocessable_entity }
        format.js
      end
    end
end

def destroy
    @status = current_member.statuses.find(params[:id])
    @activity = Activity.find_by_targetable_id(params[:id])
    @commentable = @status
    @comments = @commentable.comments
    if @activity
      @activity.destroy
    end
    if @comments
      @comments.destroy
    end 
    @status.destroy

    respond_to do |format|
      format.html { redirect_to profile_path(current_member) }
      format.json { head :no_content }
    end
end

private

def find_member
    @member = Member.find_by_user_name(params[:user_name])
end 

def find_status
    @status = current_member.statuses.find_by_permalink(params[:id])
end  

end

comments_controller.rb

class CommentsController < ApplicationController

before_filter :authenticate_member!
before_filter :load_commentable
before_filter :find_member

def index
    redirect_to root_path
end

def new
    @comment = @commentable.comments.new
end

def create
    @comment = @commentable.comments.new(params[:comment])
    @comments = @commentable.comments.order('created_at desc').page(params[:page]).per_page(15)
    @comment.member = current_member
    respond_to do |format|
      if @comment.save
        format.html { redirect_to :back }
        format.json
        format.js
      else
        format.html { redirect_to :back }
        format.json
        format.js
      end
    end 
end

def destroy
    @comment = Comment.find(params[:id])
    respond_to do |format|
      if @comment.member == current_member || @commentable.member == current_member
        @comment.destroy
        format.html { redirect_to :back }
        format.json
        format.js
      else
        format.html { redirect_to :back, alert: 'You can\'t delete this comment.' }
        format.json
        format.js
      end
    end 
end

private

# def load_commentable
#       resource, id = request.path.split('/')[1,2] # photos/1/
#       @commentable = resource.singularize.classify.constantize.find(id) # Photo.find(1)
# end 

# alternative option:
def load_commentable
    klass = [Status, Medium, Project, Event, Listing].detect { |c| params["#{c.name.underscore}_id"] }
    @commentable = klass.find(params["#{klass.name.underscore}_id"])
end

#def load_commentable
#  @commentable = params[:commentable_type].camelize.constantize.find(params[:commentable_id])
#end

def find_member
    @member = Member.find_by_user_name(params[:user_name])
end 

end

问题在于load_commentable中的comments_controller方法。我已经尝试了几种不同的方法,但第二种方法最适合我的应用程序,当网址中有自己的ID时,它就可以正常工作。但是由于我覆盖了to_param以使用我的随机永久链接评论停止工作,因为它试图找到id,它等于permalink。由于它似乎试图通过网址找到ID,我如何传递实际ID而不是固定链接,或者如何通过它的永久链接而不是ID找到commentable

1 个答案:

答案 0 :(得分:1)

很难判断你的param是永远是id的值还是永远是永久链接,或者有时候是id,有时候是永久链接。

如果它永远是永久链接,那么请执行:

@commentable = klass.find_by_permalink(params["#{klass.name.underscore}_id"])

而不是

@commentable = klass.find(params["#{klass.name.underscore}_id"])

如果它有时是id而有时是其他的,那么你需要make逻辑来确定基于类需要哪个。