0

Rails 3.2.3

我正在尝试向我的 Rails 应用程序添加“点”功能。这个想法很简单,如果你点击UP你的积分会增加 1。如果你点击DOWN你的积分会减少 1。但由于某种原因,积分不会改变,我创建的每个新链接都会增加 10。

我的迁移文件中的点默认为 0(但它们从不显示为零)

t.column :points, :integer, :default => 0`

这是我的视图代码的一部分

<td>
  <%= link_to "Up",
                    :url => { :action => 'modify_points',
                               :id => link.id,
                               :by => 1 }, 
                               :remote => true,
                    :update => "link" + link.id.to_s %>
</td>
<td>
  <%= link_to "Down",
                    :url => { :action => 'modify_points',
                              :id => link.id,
                              :by => -1 },
                              :remote => true,
                    :update => "link" + link.id.to_s %>

</td>

<td>
  <b> <a href="<%= link.url %>"> <%= link.description %> </a> </b>
  (<%= if link.url =~ /:\/\/([^\/]*)/ then $1 else "wrong URL" end %>) <br/>
  <span id="link"><%= link.id.to_s %><%= link.points %> </span>
  points posted <%= time_ago_in_words link.created_at %> ago
</td>

这是我modify_points在 links_controller.rb 中的方法:

    def modify_points
      @link = Link.find(params[:id])
      @link.update_attribute :points, @link.points + params[:by].to_i if params[:by] =~ /[+|-]?1/
      render_text @link.points
    end

现在,我第一个提交的链接有 10 分,我的第 2 个链接有 20 分,我的第 3 个链接有 30 分……等等……等等……而UP&DOWN链接什么也不做。为什么我看不到默认点值 0?方法中的正则表达式代码是否会modify_points导致这些错误?

**编辑:这是我按下 UP 链接时的 RAILS 服务器日志**

Processing by LinksController#submissions as HTML
   (0.1ms)  SELECT COUNT(*) FROM "links" 
  Link Load (0.3ms)  SELECT "links".* FROM "links" LIMIT 20 OFFSET 0
  Rendered links/submissions.html.erb within layouts/application (145.9ms)
Completed 200 OK in 435ms (Views: 316.5ms | ActiveRecord: 9.4ms)


Started GET "/assets/links.css?body=1" for 127.0.0.1 at 2012-06-11 10:08:49 -0700
Served asset /links.css - 304 Not Modified (8ms)


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2012-06-11 10:08:51
 -0700
Served asset /application.js - 304 Not Modified (9ms)


Started GET "/assets/defaults.js" for 127.0.0.1 at 2012-06-11 10:08:51 -0700
Served asset /defaults.js - 404 Not Found (3ms)

ActionController::RoutingError (No route matches [GET] "/assets/defaults.js"):
  actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.3) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.3) lib/rails/engine.rb:479:in `call'
  railties (3.2.3) lib/rails/application.rb:220:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /Users/accraze/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
  /Users/accraze/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
  /Users/accraze/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'


  Rendered /Users/accraze/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2012-06-11 10:08:51 -0700
Served asset /application.css - 304 Not Modified (4ms)


 Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2012-06-11 10:08:51 -0700
Served asset /jquery.js - 304 Not Modified (4ms)


Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2012-06-11 10:08:51-0700
Served asset /scaffolds.css - 304 Not Modified (3ms)


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2012-06-11 10:08:51-0700
Served asset /jquery_ujs.js - 304 Not Modified (3ms)


Started GET "/assets/links.js?body=1" for 127.0.0.1 at 2012-06-11 10:08:51 -0700
Served asset /links.js - 304 Not Modified (2ms)


Started GET "/?remote=true&update=link4&url%5Baction%5D=modify_points&url%5Bby%5D=1&url%5Bid%5D=4" for 127.0.0.1 at 2012-06-11 10:08:54 -0700
Processing by LinksController#submissions as HTML
  Parameters: {"remote"=>"true", "update"=>"link4", "url"=>{"action"=>"modify_points", "by"=>"1", "id"=>"4"}}
   (0.2ms)  SELECT COUNT(*) FROM "links" 
  Link Load (0.2ms)  SELECT "links".* FROM "links" LIMIT 20 OFFSET 0
  Rendered links/submissions.html.erb within layouts/application (14.7ms)
Completed 200 OK in 21ms (Views: 20.4ms | ActiveRecord: 0.4ms)


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2012-06-11 10:08:54 -0700
Served asset /application.css - 304 Not Modified (0ms)


 Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2012-06-11 10:08:54-0700
Served asset /scaffolds.css - 304 Not Modified (0ms)


Started GET "/assets/links.css?body=1" for 127.0.0.1 at 2012-06-11 10:08:54 -0700
Served asset /links.css - 304 Not Modified (0ms)


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2012-06-11 10:08:54-0700
Served asset /application.js - 304 Not Modified (0ms)


Started GET "/assets/links.js?body=1" for 127.0.0.1 at 2012-06-11 10:08:54 -0700
Served asset /links.js - 304 Not Modified (0ms)


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2012-06-11 10:08:54 -0700
Served asset /jquery.js - 304 Not Modified (0ms)


Started GET "/assets/defaults.js" for 127.0.0.1 at 2012-06-11 10:08:54 -0700
Served asset /defaults.js - 404 Not Found (4ms)

ActionController::RoutingError (No route matches [GET] "/assets/defaults.js"):
  actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.3) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.3) lib/rails/engine.rb:479:in `call'
  railties (3.2.3) lib/rails/application.rb:220:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /Users/accraze/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
  /Users/accraze/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
  /Users/accraze/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'


  Rendered /Users/accraze/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/actionpa
ck-3.2.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb with
in rescues/layout (0.8ms)


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2012-06-11 10:08:55-
0700
Served asset /jquery_ujs.js - 304 Not Modified (0ms)
4

1 回答 1

0

好吧,您的链接不符合您的要求 - 正如您从日志中看到的那样:

Processing by LinksController#submissions as HTML
Started GET "/?remote=true&update=link4&url%5Baction%5D=modify_points&url%5Bby%5D=1&url%5Bid%5D=4" for 127.0.0.1 at 2012-06-11 10:08:54 -0700
  Processing by LinksController#submissions as HTML
  Parameters: {"remote"=>"true", "update"=>"link4", "url"=>{"action"=>"modify_points", "by"=>"1", "id"=>"4"}}

它将您的 :url 视为普通参数,并将其传递给 LinksController#submissions。您可能应该使用基于路由的助手?像这样的东西,取决于你的路线是什么样的:

<%= link_to('UP', link_modify_points_path(link, :by => 1), :remote => true) %>
于 2012-06-11T17:57:40.987 回答