我试图为我的应用创建一个类似于twitters的简单@ mentions模型。我已经开始构建它了,但我不知道如何处理提到的实际创建。我需要一些方法来扫描让我们在为任何@符号创建之前说出状态,然后针对任何匹配的用户名检查数据库后面的文本。如果匹配,则会创建提及以及状态。有人能指出我正确的方向吗?
这是我到目前为止所拥有的:
分贝/迁移/ create_mentions.rb
class CreateMentions < ActiveRecord::Migration
def change
create_table :mentions do |t|
t.belongs_to :mentionable, polymorphic: true
t.timestamps
end
add_index :mentions, [:mentionable_id, :mentionable_type]
end
end
模型/ mention.rb
class Mention < ActiveRecord::Base
belongs_to :mentionable, polymorphic: true
end
模型/ status.rb
class Status < ActiveRecord::Base
attr_accessible :content
has_many :mentions, dependent: :destroy
end
模型/ member.rb
class Member < ActiveRecord::Base
has_many :mentions, as: :mentionable, dependent: :destroy
end
控制器/ mentions_controller.rb
class MentionsController < ApplicationController
before_filter :authenticate_member!
before_filter :load_mentionable
before_filter :find_member
def new
@mention = @mentionable.mentions.new
end
def create
@mention = @mentionable.mentions.new(params[:mention])
respond_to do |format|
if @mention.save
format.html { redirect_to :back }
else
format.html { redirect_to :back }
end
end
end
private
def load_mentionable
klass = [Status].detect { |c| params["#{c.name.underscore}_id"] }
@mentionable = klass.find(params["#{klass.name.underscore}_id"])
end
def find_member
@member = Member.find_by_user_name(params[:user_name])
end
end
配置/ routes.rb中
resources :statuses do
resources :mentions
end
答案 0 :(得分:0)
感谢这个问题:parse a post for @username我能够让这个工作。我的设置:
<强>分贝/迁移/ create_mentions.rb 强>
class CreateMentions < ActiveRecord::Migration
def change
create_table :mentions do |t|
t.belongs_to :mentionable, polymorphic: true
t.belongs_to :mentioner, polymorphic: true
t.integer :status_id
t.integer :comment_id
t.timestamps
end
add_index :mentions, [:mentionable_id, :mentionable_type], :name => "ments_on_ables_id_and_type"
add_index :mentions, [:mentioner_id, :mentioner_type], :name => "ments_on_ers_id_and_type"
end
end
<强>模型/ mention.rb 强>
class Mention < ActiveRecord::Base
attr_accessible :mentioner_id, :mentioner_type, :mentionable_type, :mentionable_id, :status_id, :comment_id
belongs_to :mentioner, polymorphic: true
belongs_to :mentionable, polymorphic: true
end
<强>模型/ member.rb 强>
class Member < ActiveRecord::Base
has_many :mentions, as: :mentionable, dependent: :destroy
end
<强>模型/ status.rb 强>
class Status < ActiveRecord::Base
attr_accessor :mention
has_many :mentions, as: :mentioner, dependent: :destroy
after_save :save_mentions
USERNAME_REGEX = /@\w+/i
private
def save_mentions
return unless mention?
people_mentioned.each do |member|
Mention.create!(:status_id => self.id, :mentioner_id => self.id, :mentioner_type => 'Status', :mentionable_id => member.id, :mentionable_type => 'Member')
end
end
def mention?
self.content.match( USERNAME_REGEX )
end
def people_mentioned
members = []
self.content.clone.gsub!( USERNAME_REGEX ).each do |user_name|
member = Member.find_by_user_name(user_name[1..-1])
members << member if member
end
members.uniq
end
end
<强>配置/ routes.rb中强>
resources :statuses do
resources :mentions
end
<强>助手/ mentions_helper.rb 强>
module MentionsHelper
def statuses_with_mentions(status)
status.content_html.gsub(/@\w+/).each do |user_name|
member = Member.find_by_user_name(user_name[1..-1])
if member
link_to user_name, profile_path(member.user_name)
else
user_name
end
end
end
end