Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 计算单击链接的次数_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 计算单击链接的次数

Ruby on rails 计算单击链接的次数,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,因此,我试图记录一个链接被点击的次数,但无法越过最后一个障碍 到目前为止,我有以下几点: config/routes.rb resources :papers do resources :articles do resources :clicks end end class Click < ActiveRecord::Base belongs_to :article, counter_cache: true validates

因此,我试图记录一个链接被点击的次数,但无法越过最后一个障碍

到目前为止,我有以下几点:

config/routes.rb

resources :papers do 

    resources :articles do

        resources :clicks
    end 
end
class Click < ActiveRecord::Base

    belongs_to :article, counter_cache: true

    validates :ip_address, uniqueness: {scope: :article_id}
end
class Article < ActiveRecord::Base


  has_many :clicks

end
  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
点击.rb

resources :papers do 

    resources :articles do

        resources :clicks
    end 
end
class Click < ActiveRecord::Base

    belongs_to :article, counter_cache: true

    validates :ip_address, uniqueness: {scope: :article_id}
end
class Article < ActiveRecord::Base


  has_many :clicks

end
  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
article.rb

resources :papers do 

    resources :articles do

        resources :clicks
    end 
end
class Click < ActiveRecord::Base

    belongs_to :article, counter_cache: true

    validates :ip_address, uniqueness: {scope: :article_id}
end
class Article < ActiveRecord::Base


  has_many :clicks

end
  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——文章

<% @articles.each do |article| %>

   <div class="articles col-md-4">

   <%= link_to article.url,  target: '_blank' do %>

   <h4><%= article.title %></h4>
   <h5><%= article.paper.name.upcase %></h5>
   <h6><%= article.created_at.strftime("%d %B %y") %></h6>
<% end %>

首先,这个设置看起来正确吗?有人看到我哪里出了问题吗? 其次,我不知道如何设置我的视图,以便在单击现有链接时注册单击并增加计数


谢谢

在我看来,你应该做两件事:

1)将“点击”的所有方法设置到一个模型中

例如,您可以删除
单击控制器
,然后添加以下内容:

class Article
  def create_click(ip_address)
    self.clicks.create({ :ip_address => ip_address })
  end
end
这段代码需要注意一点:代码中有一个唯一性验证。实际上,当一篇文章和一个ip地址已经存在点击时,
create
方法将返回false。不要使用
create,否则将引发异常

2)添加筛选器:

您只需在
ArticlesController
中添加一个过滤器即可。在每次
显示时
,它将为所查看的
文章创建一个
单击
实例

class ArticlesController
  before_filter :create_click, :only => [ :show ]

  def create_click
     @article.create_click(ip_address)
  end
end

用以下方法解决

单击控制器.rb

resources :papers do 

    resources :articles do

        resources :clicks
    end 
end
class Click < ActiveRecord::Base

    belongs_to :article, counter_cache: true

    validates :ip_address, uniqueness: {scope: :article_id}
end
class Article < ActiveRecord::Base


  has_many :clicks

end
  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
原件:

def create

        @article = Article.find(params[:article_id])

        @click = @article.clicks.new(ip_address: request.ip)

        @click.save

    end
end
<%= link_to article.url,  target: '_blank' do %>
修正案:

def create

        @article = Article.find(params[:article_id])

        @click = @article.clicks.new(ip_address: request.ip)

        @click.save

        redirect_to @article.url


    end
end
<%= link_to paper_article_views_path(article.id, article), method: :post,  target: '_blank' do %>
index.html.erb——文章

<% @articles.each do |article| %>

   <div class="articles col-md-4">

   <%= link_to article.url,  target: '_blank' do %>

   <h4><%= article.title %></h4>
   <h5><%= article.paper.name.upcase %></h5>
   <h6><%= article.created_at.strftime("%d %B %y") %></h6>
<% end %>
原件:

def create

        @article = Article.find(params[:article_id])

        @click = @article.clicks.new(ip_address: request.ip)

        @click.save

    end
end
<%= link_to article.url,  target: '_blank' do %>

修正案:

def create

        @article = Article.find(params[:article_id])

        @click = @article.clicks.new(ip_address: request.ip)

        @click.save

        redirect_to @article.url


    end
end
<%= link_to paper_article_views_path(article.id, article), method: :post,  target: '_blank' do %>


另外,我编辑了原始问题,将
routes.rb
文件包括在内。

谢谢@遗忘形态,我实际上刚刚通过将原始链接替换为
实现了这一点,但我仍然不明白为什么我需要给出route
article.id
article
??我通过反复试验找到了答案。。Thanks@James编辑您的原始消息并共享您的
config/routes
,您的路由很奇怪。我添加了
routes.rb
。你是说路由嵌套的方式是我必须给路由指定
article.id
article
的原因吗?干杯,我现在明白了。它不是
article.id
,而是
paper
article.paper
。要理解,请在控制台中运行
rake routes
。你会得到这样一行:
paper\u-article-get/papers/:paper\u-id/articles/:id
。这意味着
paper\u article
路线应该有两个参数:a
paper
和a
article