简体   繁体   English

Rails:在参数中覆盖模型名称

[英]Rails: overriding model name in params

I have the following model: 我有以下模型:

class Action < ActiveRecord::Base
  # model code stuff
end

In the controller, I can do something like that: 在控制器中,我可以执行以下操作:

class ActionsController < ApplicationController
  # super simple update
  def update
    @action = Action.find(params[:id])
    @action.update_attributes(params[:action])
  end
end

Is it possible to make my Action model to be represented by 'something_action' in params array without renaming the model itself? 是否可以使我的Action模型在params数组中由“ something_action”表示,而不重命名模型本身? Ie so I can retrieve it like that: 即,所以我可以这样检索它:

@action.update_attributes(params[:something_action])

Any help would be appreciated? 任何帮助,将不胜感激?

在您的form_for中使用:as =>:some_other_params_name

form_for(@action, :as => :different

If you are using form_for , you can change it to 如果您使用form_for ,则可以将其更改为

form_for :something_action, @action do |f|

so it uses :something_action as the name of the params 所以它使用:something_action作为参数的名称

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

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