2014-09-18 72 views
0

我實現一個投票系統,在我的應用程序的帖子我得到,我有困難的診斷語法錯誤語法錯誤,意外「(」,期待keyword_end before_action:update_vote(NEW_VALUE)

此!。是錯誤的,我軌服務器副本:

Started POST "/posts/44/up-vote" for 127.0.0.1 at 2014-09-18 12:48:44 -0400 

SyntaxError - syntax error, unexpected '(', expecting keyword_end 
    before_action :update_vote!(new_value) 
           ^: 
    app/controllers/votes_controller.rb:3:in `' 
    activesupport (4.0.5) lib/active_support/dependencies.rb:424:in `block in load_file' 
    activesupport (4.0.5) lib/active_support/dependencies.rb:616:in `new_constants_in' 
    activesupport (4.0.5) lib/active_support/dependencies.rb:423:in `load_file' 
    activesupport (4.0.5) lib/active_support/dependencies.rb:324:in `require_or_load' 
    activesupport (4.0.5) lib/active_support/dependencies.rb:463:in `load_missing_constant' 
    activesupport (4.0.5) lib/active_support/dependencies.rb:184:in `const_missing' 
    activesupport (4.0.5) lib/active_support/inflector/methods.rb:226:in `block in constantize' 
    activesupport (4.0.5) lib/active_support/inflector/methods.rb:224:in `constantize' 
    activesupport (4.0.5) lib/active_support/dependencies.rb:535:in `get' 
    activesupport (4.0.5) lib/active_support/dependencies.rb:566:in `constantize' 
    actionpack (4.0.5) lib/action_dispatch/routing/route_set.rb:76:in `controller_reference' 
    actionpack (4.0.5) lib/action_dispatch/routing/route_set.rb:66:in `controller' 
    actionpack (4.0.5) lib/action_dispatch/routing/route_set.rb:44:in `call' 
    actionpack (4.0.5) lib/action_dispatch/journey/router.rb:71:in `block in call' 
    actionpack (4.0.5) lib/action_dispatch/journey/router.rb:59:in `call' 
    actionpack (4.0.5) lib/action_dispatch/routing/route_set.rb:674:in `call' 
    warden (1.2.3) lib/warden/manager.rb:35:in `block in call' 
    warden (1.2.3) lib/warden/manager.rb:34:in `call' 
    rack (1.5.2) lib/rack/etag.rb:23:in `call' 
    rack (1.5.2) lib/rack/conditionalget.rb:35:in `call' 
    rack (1.5.2) lib/rack/head.rb:11:in `call' 
    actionpack (4.0.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call' 
    actionpack (4.0.5) lib/action_dispatch/middleware/flash.rb:241:in `call' 
    rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context' 
    rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call' 
    actionpack (4.0.5) lib/action_dispatch/middleware/cookies.rb:486:in `call' 
    activerecord (4.0.5) lib/active_record/query_cache.rb:36:in `call' 
    activerecord (4.0.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call' 
    activerecord (4.0.5) lib/active_record/migration.rb:373:in `call' 
    actionpack (4.0.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' 
    activesupport (4.0.5) lib/active_support/callbacks.rb:373:in `_run__3205659148210679757__call__callbacks' 
    activesupport (4.0.5) lib/active_support/callbacks.rb:80:in `run_callbacks' 
    actionpack (4.0.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call' 
    actionpack (4.0.5) lib/action_dispatch/middleware/reloader.rb:64:in `call' 
    actionpack (4.0.5) lib/action_dispatch/middleware/remote_ip.rb:76:in `call' 
    better_errors (2.0.0) lib/better_errors/middleware.rb:84:in `protected_app_call' 
    better_errors (2.0.0) lib/better_errors/middleware.rb:79:in `better_errors_call' 
    better_errors (2.0.0) lib/better_errors/middleware.rb:57:in `call' 
    actionpack (4.0.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call' 
    actionpack (4.0.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' 
    railties (4.0.5) lib/rails/rack/logger.rb:38:in `call_app' 
    railties (4.0.5) lib/rails/rack/logger.rb:20:in `block in call' 
    activesupport (4.0.5) lib/active_support/tagged_logging.rb:68:in `block in tagged' 
    activesupport (4.0.5) lib/active_support/tagged_logging.rb:26:in `tagged' 
    activesupport (4.0.5) lib/active_support/tagged_logging.rb:68:in `tagged' 
    railties (4.0.5) lib/rails/rack/logger.rb:20:in `call' 
    actionpack (4.0.5) lib/action_dispatch/middleware/request_id.rb:21:in `call' 
    rack (1.5.2) lib/rack/methodoverride.rb:21:in `call' 
    rack (1.5.2) lib/rack/runtime.rb:17:in `call' 
    activesupport (4.0.5) lib/active_support/cache/strategy/local_cache.rb:83:in `call' 
    rack (1.5.2) lib/rack/lock.rb:17:in `call' 
    actionpack (4.0.5) lib/action_dispatch/middleware/static.rb:64:in `call' 
    rack (1.5.2) lib/rack/sendfile.rb:112:in `call' 
    railties (4.0.5) lib/rails/engine.rb:511:in `call' 
    railties (4.0.5) lib/rails/application.rb:97:in `call' 
    rack (1.5.2) lib/rack/lock.rb:17:in `call' 
    rack (1.5.2) lib/rack/content_length.rb:14:in `call' 
    rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service' 
    /Users/louismorin/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service' 
    /Users/louismorin/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run' 
    /Users/louismorin/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread' 

這是我votes_controller.rb文件

class VotesController < ApplicationController 
    before_action :load_post_and_vote 
    before_action :update_vote!(new_value) 


    def up_vote 
     # http://apidock.com/rails/ActionController/Base/redirect_to 
     update_vote(:value, 1) 
     redirect_to :back 
    end 

    def down_vote 
     update_vote(:value, -1) 
     redirect_to :back 
    end 

    private 

    def load_post_and_vote 
     # extract into this method the definition of @post and @vote 
     @post = Post.find(params[:post_id]) 
     @vote = @post.votes.where(user_id: current_user.id).first 
    end 

    def update_vote!(new_value) 
     # Extract vote updating/creating logic here. 
    # This method has access to @vote, because of the before_action 
    if @vote 
      authorize @vote, :update? 
      @vote.update_attribute(:value, 1) 
     else 
      @vote = current_user.votes.build(value: 1, post: @post) 
      authorize @vote, :create? 
      @vote.save 
    end 
    end 


end 
+1

您的'update_vote!'方法只有一個參數,但您在控制器代碼中調用了兩個參數。由於它是在適用的控制器操作中直接調用的,爲什麼不省略這個'before_action'? – tadman 2014-09-18 17:02:51

+0

看看這個鏈接是否可以幫助你[5507026 - before-filter-with-parameters](http://stackoverflow.com/questions/5507026/before-filter-with-parameters)。 – yanksan 2014-09-18 17:09:57

回答

1

before_action給出的方法名稱作爲符號(或一個集團。 k,如在另一個答案中)。你不能像你嘗試的那樣爲這個方法提供一個參數。 (該參數從哪裏來?)

與之前的動作一樣,update_vote和up_vote自動重定向,意味着他們永遠不會讓你觸及你想要調用的動作。

此外,您在顯式調用兩個動作之前調用update_vote。

我認爲你在調用update vote作爲before操作時犯了一個錯誤。要修復/避免語法錯誤,我會刪除該行。無論如何,在你的兩個行動中,更新投票無論如何都會被調用,並帶有爭論。

如果您想要在創建後立即發生的行爲,我會在模型中添加該行爲。

+0

嗨Sasha,如果我拿出update_vote之前的動作,然後我得到一個沒有方法錯誤'未定義的方法'update_vote'爲#' – 2014-09-18 18:50:15

+0

你錯過了一個'!' – Sasha 2014-09-18 19:52:25

相關問題