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これらのエラーの原因ですか?

**編集:アップリンクを押したときの私の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 に答える