link_to ... remote:true,方法:post总是在第一次尝试时失败

时间:2014-09-07 08:13:43

标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-4.1

我有一个link_to使用remote:true通过POST对特定功能进行切换,在点击时切换它的状态。但是,每当我尝试切换它时,rails仅在第一次尝试时响应404,但它在第二次尝试时正常工作。该路线正常工作和配置,但不知道为什么第一次点击总是返回404.这发生在Chrome和Firefox上,仅限生产,开发工作正常。

我在Ubuntu上使用Rails 4.1和Ruby 2.1.1p76,Unicorn + Nginx

这是日志消息:

 I, [2014-09-07T07:51:27.251225 #18157]  INFO -- : Started POST "/u/alexandre/recommend/gigantes-de-aco-e-pontas-soltas" for 101.138.232.14 at 2014-09-07 07:51:27 +0000
F, [2014-09-07T07:51:27.254116 #18157] FATAL -- : 
ActionController::RoutingError (No route matches [POST] "/u/alexandre/recommend/gigantes-de-aco-e-pontas-soltas"):
  actionpack (4.1.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.0) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.0) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.0) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.0) 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.1.0) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.0) lib/rails/engine.rb:514:in `call'
  railties (4.1.0) lib/rails/application.rb:144:in `call'
  unicorn (4.8.3) lib/unicorn/http_server.rb:576:in `process_client'
  unicorn (4.8.3) lib/unicorn/http_server.rb:670:in `worker_loop'
  unicorn (4.8.3) lib/unicorn/http_server.rb:525:in `spawn_missing_workers'
  unicorn (4.8.3) lib/unicorn/http_server.rb:536:in `maintain_worker_count'
  unicorn (4.8.3) lib/unicorn/http_server.rb:294:in `join'
  unicorn (4.8.3) bin/unicorn_rails:209:in `<top (required)>'
  /usr/local/rvm/gems/ruby-2.1.1/bin/unicorn_rails:23:in `load'
  /usr/local/rvm/gems/ruby-2.1.1/bin/unicorn_rails:23:in `<main>'
  /usr/local/rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `eval'
  /usr/local/rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `<main>'


I, [2014-09-07T07:51:31.251047 #6505]  INFO -- : Started POST "/u/alexandre/recommend/gigantes-de-aco-e-pontas-soltas" for 101.138.232.14 at 2014-09-07 07:51:31 +0000
I, [2014-09-07T07:51:31.254408 #6505]  INFO -- : Processing by UsersController#recommend as JS"/u/alexandre/recommend/gigantes-de-aco-e-pontas-soltas" for 101.138.232.14 at 2014-09-07 07:51:31 +0000

的routes.rb

resources :users, :param => :id, constraints: {id: /.*/}, only: [:new, :create, :edit, :update], path: "u" do
    post "/recommend/:text_id/" => "users#recommend", as: "recommend"
end

_bookshelf_actions.html.haml

%ul#bookshelf-actions
  %li#recommend.is-action.text-center
    %span#recommendation-count.count= text.recommendations_count
    = link_to '', recommend_url , remote: true, method: :post, id: "recommend-btn", class: 'reader-social-icon social-action is-recommend', data: {recommended: recommended}
    %span#recommendation-status.action-name= t(:recommend, count: @current_user ? (@current_user.recommended?(text) ? 1 : 0) : 0)

text.js.coffee

@text.actions.recommend.on 'ajax:success', (e, data) ->
    self.text.actions.recommend.attr 'data-recommended', data.recommended
    $('#recommendation-status').text data.status
    $('#recommendation-count').text data.counter
    return

users_controller.rb

def recommend
    text = Text.friendly.find(params[:text_id])
    @user.recommend(text)
    respond_to do |format|
      status = I18n.t(:recommend, count: @user.recommended?(text) ? 1 : 0)
      text.reload
      counter = text.recommendations_count
      format.json {render json: {status: status, recommended: @user.recommended?(text), counter: counter}, status: :ok}
    end
  end

我已经google了一下,并没有发现任何关于这个问题或类似的东西。任何帮助将非常感谢! 感谢

1 个答案:

答案 0 :(得分:2)

您的路线有问题,我没有找到在资源中没有membercollection的路线的文档。使用您的配置路由,链接可能匹配:

u/recommend/:text_id

所以你应该使用:

member do 
  post "/recommend/:text_id/" => "users#recommend", as: "recommend" 
end

在您的资源路线中。

我认为它有错误,因为路由错过了用户资源的id。使用member do ... end将匹配您的预期路线:

/u/:id/recommend/:text_id