所以我试图记录链接被点击但无法克服最后一道障碍的次数。
到目前为止,我有以下内容:
配置/ routes.rb中
resources :papers do
resources :articles do
resources :clicks
end
end
click.rb
class Click < ActiveRecord::Base
belongs_to :article, counter_cache: true
validates :ip_address, uniqueness: {scope: :article_id}
end
clicks_controller.rb
class ClicksController&lt; ApplicationController中
def create
@article = Article.find(params[:article_id])
@click = @article.clicks.new(ip_address: request.ip)
@click.save
end
end
article.rb
class Article < ActiveRecord::Base
has_many :clicks
end
schema.rb
create_table "clicks", force: true do |t|
t.integer "article_id"
t.string "ip_address"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "articles", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.text "title"
t.string "url"
t.integer "paper_id"
t.integer "clicks_count"
end
index.html.erb - 文章
首先,这个设置看起来是否正确,有人看到我可能出错的地方吗?
其次,我不知道如何设置我的视图,以便在点击现有链接时注册点击并且计数上升?
由于