1

내 앱에는 주로이 railscast을 기반으로하는 댓글 달기 시스템이 있습니다. 이제는 내 모델에서 to_param을 임의의 문자열로 변경하여 ID가 ​​URL에 없도록합니다. 그러나 그 다음에는 논평이 중단됩니다.퍼시픽 링크/토큰 URL을 사용하여 다형성으로 설명하는 레일

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 

T 그는 문제가 의 load_commentable 메서드에 있습니다. 메소드의 몇 가지 다른 변형을 시도했지만 두 번째 앱은 내 앱에 가장 적합하며 URL에 ID가있을 때 작동합니다. 그러나 내가 to_param을 겹쳐 쓸 때 무작위 퍼머 링크의 주석 달기 기능은 idpermalink과 같은 것을 찾기 때문에 주석 처리가 중단되었습니다. URL을 통해 ID를 찾으려고 시도하는 것처럼 보이지만 실제 ID가 아닌 퍼머 링크를 전달하려면 어떻게해야합니까? 아니면 id 대신 permalink를 사용하여 commentable을 찾을 수 있습니까?

답변

1

param이 항상 id의 값이거나 항상 퍼머 링크인지 또는 때때로 ID와 때로는 퍼머 링크인지 알기는 어렵습니다. 그것은 항상 퍼머를 할 경우

, 다음을 수행하십시오 대신

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

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

가끔 ID입니다 때때로, 당신은 논리가 결정 할 수 있도록 다른 필요한 경우 클래스에 따라 필요합니다.

+0

감사합니다. 다시 올바르게 작동합니다. 항상 영구 링크가되어 필요한 항목입니다. – iamdhunt