带有永久链接/令牌URL的Rails多态注释

iamdhunt

在我的应用程序中,我有一个注释系统,该系统主要基于此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我尝试了几种方法的不同变体,但是第二种方法最适合我的应用程序,并且当URL中包含其ID时,它才起作用。但是由于我重写了to_param使用我的随机永久链接的注释,因此它停止了工作,因为它试图查找id等于的位置permalink由于它似乎试图通过url查找id,因此如何传递实际的id,而不是永久链接,或者如何commentable通过其永久链接而不是id进行查找

像素地球

很难确定您的参数将始终是id的值还是始终是永久链接,或者有时是id有时是永久链接。

如果它将始终是永久链接,请执行以下操作:

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

代替

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

如果它有时是id有时是id,那么您将需要基于类确定逻辑来确定需要哪个。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用带有Rails URL链接的代理

来自分类Dev

Rails 中带有自定义操作的多态路由

来自分类Dev

Wordpress URL路由,具有不同模板的多个永久链接

来自分类Dev

如何编写带有可链接令牌的宏?

来自分类Dev

如何编写带有可链接令牌的宏?

来自分类Dev

带有Devise on Rails 3的注销链接

来自分类Dev

带有Devise on Rails 3的注销链接

来自分类Dev

Rails-电子邮件链接验证和带有URL验证的电话号码验证

来自分类Dev

附加Wordpress永久链接URL

来自分类Dev

RewriteRule获取带有URL的链接

来自分类Dev

如何获取Shopify(Rails)的永久令牌?

来自分类Dev

如何获得Shopify(Rails)的永久令牌?

来自分类Dev

使用带有永久链接的自定义插件重写网址

来自分类Dev

大量的Wordpress Pages是否会使带有/%postname%/永久链接结构的网站变慢?

来自分类Dev

带有Paperclip的Rails 4多态图像上传不适用于所有模型

来自分类Dev

带有“#”或“#:”的python注释

来自分类Dev

带有allowAll()和过期的Auth令牌的URL的Spring Security

来自分类Dev

带有根的永久终端

来自分类Dev

Rails4的页面和用户(类似于Facebook)的URL永久链接。Friendly_id?

来自分类Dev

带有注释的Boost Spirit x3令牌生成器不起作用

来自分类Dev

如何将带有链接注释的覆盖文本添加到现有pdf?

来自分类Dev

如何创建带有Rails的pdf文件的链接?

来自分类Dev

在Rails内部带有参数的自定义链接

来自分类Dev

带有Devise的Rails 3.2:注销链接停止工作

来自分类Dev

如何使用带有Rails 4的Foundation紧密链接?

来自分类Dev

启用永久链接的JavaScript URL问题

来自分类Dev

CI 控制器 url 永久链接

来自分类Dev

Rails多态注释未定义方法user_name

来自分类Dev

无法在Rails多态注释,ActiveRecord中实现销毁

Related 相关文章

  1. 1

    使用带有Rails URL链接的代理

  2. 2

    Rails 中带有自定义操作的多态路由

  3. 3

    Wordpress URL路由,具有不同模板的多个永久链接

  4. 4

    如何编写带有可链接令牌的宏?

  5. 5

    如何编写带有可链接令牌的宏?

  6. 6

    带有Devise on Rails 3的注销链接

  7. 7

    带有Devise on Rails 3的注销链接

  8. 8

    Rails-电子邮件链接验证和带有URL验证的电话号码验证

  9. 9

    附加Wordpress永久链接URL

  10. 10

    RewriteRule获取带有URL的链接

  11. 11

    如何获取Shopify(Rails)的永久令牌?

  12. 12

    如何获得Shopify(Rails)的永久令牌?

  13. 13

    使用带有永久链接的自定义插件重写网址

  14. 14

    大量的Wordpress Pages是否会使带有/%postname%/永久链接结构的网站变慢?

  15. 15

    带有Paperclip的Rails 4多态图像上传不适用于所有模型

  16. 16

    带有“#”或“#:”的python注释

  17. 17

    带有allowAll()和过期的Auth令牌的URL的Spring Security

  18. 18

    带有根的永久终端

  19. 19

    Rails4的页面和用户(类似于Facebook)的URL永久链接。Friendly_id?

  20. 20

    带有注释的Boost Spirit x3令牌生成器不起作用

  21. 21

    如何将带有链接注释的覆盖文本添加到现有pdf?

  22. 22

    如何创建带有Rails的pdf文件的链接?

  23. 23

    在Rails内部带有参数的自定义链接

  24. 24

    带有Devise的Rails 3.2:注销链接停止工作

  25. 25

    如何使用带有Rails 4的Foundation紧密链接?

  26. 26

    启用永久链接的JavaScript URL问题

  27. 27

    CI 控制器 url 永久链接

  28. 28

    Rails多态注释未定义方法user_name

  29. 29

    无法在Rails多态注释,ActiveRecord中实现销毁

热门标签

归档