使用form_for和.new时如何防止污染Rails对象? [英] How to prevent polluting a rails object when using form_for and .new?

查看:76
本文介绍了使用form_for和.new时如何防止污染Rails对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模特:

class Activity < ActiveRecord::Base
    has_many   :activity_states
end

我想为新的活动状态呈现选项,并呈现现有的活动状态.但是,当我使用此form_for:

I want to render options for new activity states and render existing activity states. However when I use this form_for:

<%= form_for activity.activity_states.new, :remote => true do |f| %>
    <%= f.hidden_field :state_type, :value => activity_state_type %>
    <%= f.hidden_field :activity_id %>
    <%= f.submit submit_label, :disable_with => 'sending...' %>
<% end %>

用于创建新的临时activity_state模型以用作新的activity_states的基础,它会污染具有nil活动状态的活动对象,因此将具有活动状态的Activity代替:

for creating new temporary activity_state models to use as the basis for new activity_states it pollutes the activity object with nil activity states so instead of Activity having the activity states:

[#<ActivityState id: 7353, activity_id: 2033, state_type: 0, created_at: "2012-05-31 11:06:50">, 
#<ActivityState id: 7354, activity_id: 2033, state_type: 1, created_at: "2012-05-31 11:06:53">, 
#<ActivityState id: 7355, activity_id: 2033, state_type: 2, created_at: "2012-05-31 11:06:59">]

由于.new,它在渲染过程中还附加了几个这些nil activity_state对象:

It also has several these nil activity_state objects appended mid render because of the .new:

#<ActivityState id: nil, activity_id: 2033, state_type: nil, created_at: nil>

导致:

[#<ActivityState id: 7353, activity_id: 2033, state_type: 0, created_at: "2012-05-31 11:06:50">, 
#<ActivityState id: 7354, activity_id: 2033, state_type: 1, created_at: "2012-05-31 11:06:53">, 
#<ActivityState id: 7355, activity_id: 2033, state_type: 2, created_at: "2012-05-31 11:06:59">,
#<ActivityState id: nil, activity_id: 2033, state_type: nil, created_at: nil>,
#<ActivityState id: nil, activity_id: 2033, state_type: nil, created_at: nil>]

然后当我遍历活动的activity_states并呈现它们时,这会弄乱.我尝试了一种骇人听闻的方法来解决此问题,方法是:在每个form_for之后使用<% activity.activity_states.pop %>,但是在Heroku上进行生产时,该方法不起作用,因为它抱怨要尝试修改冻结的数组.

Which then messes things up when I loop over the activity_states for the activity and render them. I tried a hacky way of getting around this using: <% activity.activity_states.pop %> after each form_for but this doesn't work when in production on Heroku as it complains about trying to modify a frozen array.

请问有任何关于正确"方法的想法吗?我知道我无法理解这里的一些基本知识,也知道我通过为id字段插入一个隐藏字段来打开一个漏洞(

Any ideas on the "right" way of doing this please? I know I'm failing to understand something pretty fundamental here and also know I've opened up a hole by inserting a hidden field for the id field (which can then be manipulated by malicious users).

推荐答案

到目前为止,我有一个解决方案,在activity controllershow方法中,包括:

I have one solution so far, in the show method of the activity controller, include:

@new_activity_state = @activity.activity_states.new
@activity.activity_states.pop

以及局部视图(我将@activity作为局部变量activity传递):

and in the view partial (which I pass @activity as the local variable activity):

<% new_activity_state = @new_activity_state || activity.activity_states.new %>
<%= form_for new_activity_state, :remote => true do |f| %>
    <%= f.hidden_field :state_type, :value => activity_state_type %>
    <%= f.hidden_field :activity_id %>
    <%= f.submit submit_label, :disable_with => 'sending...' %>
<% end %>

Heroku的生产环境现在不反对.pop,这很不错:D但是有更好的方法吗?

Heroku's production environment doesn't object to the .pop now which is great :D But is there a better way?

这篇关于使用form_for和.new时如何防止污染Rails对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆