2012-08-31 77 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。添加他們,一切都很好。

相關問題