2012-08-31 3 views
0

이것은 일반적인 오류 인 것 같습니다. 많은 답글을 읽었습니다. 나는 바보 같은 것을 놓치고있을거야.rails : 보호 된 속성을 대량 지정할 수 없습니다.

한 곳에서 활동하고 있습니다. (Has_one). accepts_nested_attributes_for를 설정했습니다. 위치는 단수/복수 (모든 단수, 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 

간단해야하지만 분명하지 않습니다.

미리 감사드립니다. 당신이 위치에 대한 fields_for 루프를 생략 할 수도 것처럼 보이는 오류를 중첩 된 필드와 양식을 보지 않고

<%= 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

- 그것은

<%= f.fields_for :location do |f| %> 
    <%= render :partial => "locations/form", :locals => {:f => f} %> 
<% end -%> 
처럼 보일 것이다 :

관련 양식 코드 추가

중첩 된 필드의 양식을로드하고 작업의 위치에 대한 특성을 설정하지 않도록해야합니다.

+0

이 (가)보기를 표시하도록 편집되었습니다. 나는 fields_for를 이미 사용했지만 어쨌든 잘못 생각한 적이 있습니까? – joelm

0
레일 3에서

최대, 당신은 attr_writer 또는 attr_accessor, 질량 할당 attr_accessible 당신이 이러한 특성

주소, LOCATION_NAME, PHONE_NUMBER, 지구, 우편 번호, 도시에 대한 몇 가지 쓰기 권한을 만들어야합니다

속성이 필요합니다 , country, lat,

+0

attr_accessible : location_attributes를 추가한다고 생각하나요? 나는 그것을 Activity 모델에 가지고있다. 나는 그것들 각각에 대해 att_accessible을 추가했지만, 도움이되지 않았다. – joelm

0

밝혀졌습니다. 모든 속성에 대한 위치 모델에서 attr_accessible이 누락되었습니다. 그들을 추가하고 모두 잘됩니다.

관련 문제