无法让ActiveRecord :: Store以表格形式工作

时间:2012-12-21 13:05:27

标签: ruby-on-rails ruby-on-rails-3.2

在3.2的更高版本中是否更改了ActiveRecord :: Store?我在搜索中找不到很多相关内容。有些人认为很棒,有些人认为 意见。有一些早期的3.2发布博客条目,大多数只是使用控制台来更新ActiveRecord :: Store中的属性。有 一些 examples / tutorials 将其与表单一起使用。大多数使用商店属性就像一个列,但我不能让它工作。也无法让一些控制台示例起作用。

class Stage < ActiveRecord::Base
  AD_ATTR = [:job_area, :end_date, :ad_url, :instructions, :other]
  store :ad, :assessors => AD_ATTR
  attr_accessible *AD_ATTR, :date, :enterable, :est_candidates, :name, :status, :program_id, :sequence
end

# I've also tried it without the * and defining each accessor

一些教程显示使用对象点表示法从控制台使用商店。我不行。使用它作为哈希工作正常。

1.9.2-p136 :752 > s = Stage.find(1)
 => #<Stage id: 1, ... , ad: {}, sequence: 1> 
1.9.2-p136 :753 > s.ad
 => {} 
1.9.2-p136 :754 > s.other
NoMethodError: undefined method `other' for #<Stage:0x00000103de4528>

1.9.2-p136 :755 > s.ad[:other]
 => nil 

1.9.2-p136 :756 > s.other = "stuff"
NoMethodError: undefined method `other=' for #<Stage:0x00000103de4528>

1.9.2-p136 :757 > s.ad[:other] = "stuff"
 => "stuff" 
1.9.2-p136 :758 > a.other
NoMethodError: undefined method `other' for #<Take::Assessment:0x000001038ba778>

然后在表格中,如果我尝试:

<tr class="field">
  <th><%= f.label :other %></th>
  <td><%= f.text_field :other %></td>
</tr>

我会得到:

undefined method 'other' for #<Stage:0x0000010396a650>

我确信我可以破解它,或者像我在其他一些地方一样使用JSON。这似乎很合适,因为只有少数舞台记录需要广告属性。

修改

无法帮助自己进行黑客攻击。我得到了它的工作,但是,根据我能找到的东西,它应该只是一个模型属性。

在表单中,我使用了fields_form:

<%= fields_for :ad_fields do |a| %>
  <tr class="field">
    <th><%= a.label :other %></th>
    <td><%= a.text_field :other, :value => @stage.ad[:other] %></td>
  </tr>

  <tr class="field">
    <th><%= a.label :job_area %></th>
    <td><%= a.text_field :job_area, :value => @stage.ad[:job_area] %></td>
  </tr>

<% end %>

在更新控制器中,我填充了商店。

def update
  @stage = Stage.find(params[:id])
  respond_to do |format|
    if params[:ad_fields]
      params[:ad_fields].each do |key,value|
        @stage.ad[key.to_sym] = value
      end
    end
    if @stage.update_attributes(params[:stage])
    ...
  end

使用此方法,您还可以将其他对象放入值中,有人已经拥有了gem store_field。我的黑客 至少会让我使用ActiveRecord :: Store

进行评估

史蒂夫

0 个答案:

没有答案