Rails 3.2.3
I'm trying to add a "points" functionality to my Rails app. The idea is pretty straight forward, if you click UP
your points will increase by 1. if you click DOWN
your points will decrease by 1. But for some reason the points do not change and each new links I create the points increase by 10.
I have the points defaulting to 0 in my migration file (but they never show zero)
t.column :points, :integer, :default => 0`
here is part of my view code
<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>
and here is my modify_points
method in my 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
right now, my first submitted link has 10 points, my 2nd link has 20 points, my 3rd link has 30 points ... etc.... etc... and the UP
& DOWN
links do nothing. Why am I not seeing the default point value of 0? Is my regex code in the modify_points
method causing these errors?
** EDIT: HERE IS MY RAILS SERVER LOG WHEN PRESSING the UP LINK **
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)
Well, your link doesn't do what you want - as you can see from the log, here:
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"}}
It's treating your :url as a normal parameter, and is passing it to LinksController#submissions. You should probably be using a route-based helper? Something like this, depending on what your routes look like:
<%= link_to('UP', link_modify_points_path(link, :by => 1), :remote => true) %>