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>
这是我的links_controller.rb中的modify_points
方法:
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个点,我的第二个链接有20个点,我的第三个链接有30个点......等等......以及UP
&amp; DOWN
个链接什么都不做。为什么我没有看到默认的点值0?我的modify_points
方法中的正则表达式代码是否会导致这些错误?
** 编辑:按下上传链接时,这是我的铁路服务器日志 **
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)
答案 0 :(得分: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#submission。您可能应该使用基于路由的帮助程序?这样的事情,取决于你的路线是什么样的:
<%= link_to('UP', link_modify_points_path(link, :by => 1), :remote => true) %>