2012-08-31 82 views
0

这似乎是一个常见错误,我读过很多回复。我必须失去一些愚蠢的东西。rails:无法批量分配受保护的属性

我有一个位置的活动。 (HAS_ONE)。我已经设置了accep_nested_attributes_for:location,我相信我有单数/复数的权利(所有单数,这是一个has_one关系)。 (我已经尝试列出每个属性的attr_accessible单独,没有骰子。)我已经重新启动我的服务器。然而,我不断收到:

无法大规模指派保护的属性:地址,LOCATION_NAME, PHONE_NUMBER,区,邮编,城市,国家,纬度,

class Activity < ActiveRecord::Base 
    attr_accessible :beer, :user, :whatdoing, :where, :with, :location_id, :location_attributes 

    has_one :location, :dependent => :destroy 

    accepts_nested_attributes_for :location 


    validates :whatdoing, :numericality => { :only_integer => true, :greater_than => -1} 
end 


class Location < ActiveRecord::Base 
    validates_presence_of :address 

    belongs_to :activity 

end 

活动控制器:

# GET /activities/new.json 
    def new 
    @activity = Activity.new 

    @activity.build_location 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @activity } 
    end 
    end 

    # PUT /activities/1 
    # PUT /activities/1.json 
    def update 
    @activity = Activity.find(params[:id]) 


    respond_to do |format| 
     if @activity.update_attributes(params[:activity]) 
     format.html { redirect_to @activity, notice: 'Activity was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @activity.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

它必须是简单的东西,但我无法发现它。

在此先感谢!

添加相关表单代码:

<%= form_for(@activity) do |activity_form| %> 
    <% if @activity.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@activity.errors.count, "error") %> prohibited this activity from being saved:</h2> 

     <ul> 
     <% @activity.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= activity_form.label :whatdoing, "Thinkin" %> 
    <%= activity_form.radio_button :whatdoing, 0 %> 
    <%= activity_form.label :whatdoing, "Drinkin" %> 
    <%= activity_form.radio_button :whatdoing, 1 %> 
    </div> 
    <div class="field"> 
    <%= activity_form.label :beer %> 
    <%= activity_form.text_field :beer %> 
    </div> 
    <div class="field"> 
    <%= activity_form.label :where %> 
    <%= activity_form.text_field :where %> 
    </div> 
    <div class="field"> 
    <%= activity_form.label :with %> 
    <%= activity_form.text_field :with %> 
    </div> 

<%= activity_form.fields_for :location do |location_fields| %> 
<div class="field search"> 
    <%= label_tag :search %> 
    <%= text_field_tag :location_search, nil, :size => 60, :type => "search" %> 
    </div> 

    <br /> 

    <div class="field"> 
    <%= location_fields.label :address, "Full Address" %> 
    <%= location_fields.text_field :address, :size => 60 %> 
    </div> 

    <div class="field"> 
    <%= location_fields.label :location_name %> 
    <%= location_fields.text_field :location_name, :size => 18 %> 

    <%= location_fields.label :phone_number %> 
    <%= location_fields.text_field :phone_number, :size => 18 %> 
    </div> 

    <div class="field"> 
    <%= location_fields.label :district %> 
    <%= location_fields.text_field :district, :size => 18 %> 
    <%= location_fields.label :postcode %> 
    <%= location_fields.text_field :postcode, :size => 6, :type => 'number' %> 
    </div> 

    <div class="field"> 
    <%= location_fields.label :city %> 
    <%= location_fields.text_field :city, :size => 18 %> 

    <%= location_fields.label :country %> 
    <%= location_fields.text_field :country, :size => 18 %> 
    </div> 

    <div class="field"> 
    <%= location_fields.label :lat %> 
    <%= location_fields.text_field :lat, :size=> 18 %> 
    <%= location_fields.label :lng %> 
    <%= location_fields.text_field :lng, :size=> 18 %> 
    </div> 
<% end %> 

    <%= render :partial => "googlemap" %> 


    <div class="actions"> 
    <%= activity_form.submit %> 
    </div> 

<% end %> 
+0

你能发布你的嵌套表单代码吗? –

回答

1

没有与嵌套的领域,从错误看起来你可能已经省略了fields_for环路位置看到您的形式 - 它应该是这个样子

<%= f.fields_for :location do |f| %> 
    <%= render :partial => "locations/form", :locals => {:f => f} %> 
<% end -%> 

这应该加载嵌套字段的表单,并确保您没有尝试设置活动位置的属性。

+0

编辑显示视图。我已经使用了fields_for,但也许错了? – joelm

0

在Rails 3,你需要attr_writer或attr_accessor,attr_accessible大规模分配属性

你要为这些属性

地址,LOCATION_NAME,PHONE_NUMBER,区,邮编,城市创建一些写权限,国家,纬度,

+0

我认为添加attr_accessible:location_attributes会做到这一点?我在Activity模型中有这个。我为每个人单独添加了att_accessible,但这并没有帮助。 – joelm

0

事实证明我错过了所有属性的位置模型的attr_accessible。添加他们,一切都很好。

相关问题