Rails Action_controller 执行完一个 action 后为什么直接执行下一个 action 了?

ziu · April 05, 2014 · Last by ziu replied at April 05, 2014 · 2136 hits
## routes.rb

  devise_for :users
  root to: "home#index"
  match '/amount', to: 'amount#index',    via: 'get'
  match '/amount', to: 'amount#process', via: 'post'

## amount_controller.rb
  def index
    @user=current_user
  end
  def process
    case params[:type]
      when  "recharge"
      @user=current_user
      user.recharge(@user,params[:num])

      when  "transfer"
      @user=current_user
      user.recharge(@user,params[:num],params[:to])

    end

##这个时候请求'0000:3000/amount'会直接跑到process,然后蹦出一个这个错误

ArgumentError in AmountController#index
wrong number of arguments (1 for 0)

##然后改成 process(arg) 将arg 打出来是"index"

为什么?

谢谢。

两个 amount,你改一个名字不一样看。是不是混淆了。

#1 楼 @chenge 将 routes.rb 改成match '/amount', to: 'amount#index', via: 'get' match '/amount/process', to: 'amount#process', via: 'post' 这样也没用,但不知你说的改是不是这个意思

ArgumentError in AmountController#index

异常是在 index action 里面抛出的。日志没贴全判断不了。

#2 楼 @ziu 换一个 match /amount2, 看有何变化

#3 楼 @Rei 日志的内容差不多就是这样 ArgumentError (wrong number of arguments (1 for 0)): app/controllers/amount_controller.rb:7:in 'process' 定位到了def process 这一行

#6 楼 @Rei 确实这样,居然起了‘process’这么敏感的词。谢啦!

貌似process 是个很基础的方法,不能覆写 (?),改成def new都可以。而且貌似 process 就是让 action 持续下去的一个函数Calls the action going through the entire action dispatch stack.

You need to Sign in before reply, if you don't have an account, please Sign up first.