简体   繁体   English

创建一个自定义方法并在rails 4的ruby中的route.rb中调用它

[英]create a custom method and call it in the routes.rb in ruby on rails 4

I am trying to do a redirect from url to another in rails 4. before doing the redirect I want to process one of the parameters and change its value in certain conditions(or not change it), then do a redirect. 我正在尝试从Rails 4中的url重定向到另一个。在执行重定向之前,我要处理参数之一并在某些条件下更改其值(或不更改它),然后执行重定向。 That's what I have tried so far 到目前为止,这就是我尝试过的

#routes.rb
get 'Client/:f_name/:l_name', to: redirect { |params, req|
        "clients_area/#{params[:f_name].parameterize}/#{my_process_params_method(params[:fname], params[:lname]) }"
      }

But I keep getting the error 但我不断收到错误

undefined method `my_process_params_method' for ActionDispatch::Routing::Mapper:0x007fe1db4e4e08> 未定义的方法`my_process_params_method'用于ActionDispatch :: Routing :: Mapper:0x007fe1db4e4e08>

What am I doing wrong here? 我在这里做错了什么? or is there a better solution? 还是有更好的解决方案?

The thing is that I must do this redirect at the level of the route and not create an action to take care of that. 问题是,我必须在路由级别执行此重定向,而不要创建操作来解决此问题。

I have realized the mistake. 我已经意识到了错误。 the method in the module should have been declared as a class method ( self.method_name ) and that the method should be called this was MyModul.my_process_params_method(var1, var2) 该模块中的方法应已声明为类方法( self.method_name ),并且应将其称为MyModul.my_process_params_method(var1, var2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM