Rails中的批量分配5

时间:2017-04-18 04:40:43

标签: ruby-on-rails-4 parameters

我尝试将comply with Rails 5.1 Gem从Rails 4升级到Rails 5.有很多代码需要修复,因为Rails 5.1不再支持直接批量分配to_bson。

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { BarcodeScanner } from '@ionic-native/barcode-scanner';

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    BrowserModule,

    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    BarcodeScanner,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

然后错误:

Mindapp::Xmain.create :service=>service,
                          :start=>Time.now,
                          :name=>service.name,
                          :ip=> get_ip,
                          :status=>'I', # init
                          :user=>current_user,
                          :xvars=> {
                              :service_id=>service.id, :p=>params,
                              :id=>params[:id],
                              :user_id=>current_user.try(:id),
                              :custom_controller=>custom_controller,
                              :host=>request.host,
                              :referer=>request.env['HTTP_REFERER']
                          }

来自此警告{{3}}:我把params(实际上不明白)放到:

DEPRECATION WARNING: Method to_bson is deprecated and will be removed in Rails 5.1, as `ActionController::Parameters` no longer inherits from hash. Using this deprecated behavior exposes potential security problems. If you continue to use this method you may be creating a security vulnerability in your app that can be exploited. Instead, consider using one of these documented methods which are not deprecated: http://api.rubyonrails.org/v5.0.2/classes/ActionController/Parameters.html

问题:这不是应该创造的。 这段代码(在修改之前)在rails 4中工作但5.0中的警告和现在在Rails 5.1中都不能使用Ruby 2.4.1。 我该怎么办/谢谢

尝试时

  Mindapp::Xmain.create(xmain_params)
  def xmain_params
            params.permit(
                          :service=>service,
                          :start=>Time.now,
                          :name=>service.name,
                          :ip=> get_ip,
                          :status=>'I', # init
                          :user=>current_user,
                          :xvars=> {
                              :service_id=>service.id, :p=>params,
                              :id=>params[:id],
                              :user_id=>current_user.try(:id),
                              :custom_controller=>custom_controller,
                              :host=>request.host,
                              :referer=>request.env['HTTP_REFERER']
                          }
            )
  end

错误:

 Mindapp::Xmain.create :service=>params.permit(service),
                      :start=>params.permit(Time.now),
                      :name=>params.permit(service.name),
                      :ip=>params.permit(get_ip),
                      :status=>params.permit('I'), # init
                      :user=>params.permit(current_user),
                      :xvars=> params.permit({
                          :service_id=>service.id, :p=>params,
                          :id=>params[:id],
                          :user_id=>current_user.try(:id),
                          :custom_controller=>custom_controller,
                          :host=>request.host,
                          :referer=>request.env['HTTP_REFERER']
                      })

尝试使用params。

Mongoid::Errors::InvalidValue - 
message:
  Value of type ActionController::Parameters cannot be written to a field of type Hash
summary:
  Tried to set a value of type ActionController::Parameters to a field of type Hash
resolution:
  Verify if the value to be set correspond to field definition:

错误:

Mindapp::Xmain.create :service=>params.service,
                      :start=>params.Time.now,
                      :name=>params.service.name,
                      :ip=> params.get_ip,
                      :status=>params('I'), # init
                      :user=>params.current_user,
                      :xvars=> {
                          :service_id=>params.service.id, :p=>params,
                          :id=>params[:id],
                          :user_id=>params.current_user.try(:id),
                          :custom_controller=>params.custom_controller,
                          :host=>params.request.host,
                          :referer=>params.request.env['HTTP_REFERER']
                      }

1 个答案:

答案 0 :(得分:0)

问题是:p=>params。我假设params是来自请求的params对象。在Rails 5.1下,它将不再支持标准哈希函数,例如to_bson,mindapp库似乎正在使用它(或者它的一个依赖库正在使用它)。

一个简单的修复可能是将其替换为:p=>params.to_unsafe_hash或更安全的解决方案:p=>params.permit(:foo, :bar)(无论应该使用哪些参数,都应该先检查/清除这些参数)。