2013-01-06 1 views
1

중첩 된 모델 양식을 사용하면 메서드 오류가 발생하지 않으며이를 파악할 수 없습니다. 어떤 도움이라도 대단히 감사하겠습니다. 동일한 양식으로 위치와 사용자를 생성하고 있습니다.중첩 된 폼에 메서드 오류가 없습니다.

오류

NoMethodError in Devise/registrations#new 

/app/views/devise/registrations/new.html.erb where line #15 raised: 

undefined method `build_location' for #<User:0x007fbafe371188> 
Extracted source (around line #15): 

12: <a class="add" href="http://www.google.com/placesforbusiness">Location not available? Click here to add it.</a> 
13: 
14: <!-- form for location info --> 
15: <% resource.build_location %> 
16: <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form-vertical' }) do |f| %> 
17: <%= f.error_notification %> 
18: <%= f.fields_for :location do |location_form| %> 

위치 모델

class Location < ActiveRecord::Base 
    has_and_belongs_to_many :users 
    accepts_nested_attributes_for :users, :allow_destroy => true 
    attr_accessible :lat, :long, :name, :street_address, :places_id, :users_attributes 
    validates_uniqueness_of :places_id, :message => "location already taken" 
    resourcify 
end 

위치 컨트롤러

def new 
    @location = Location.new 
    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @location } 
    end 
    end 

    # GET /locations/1/edit 
    def edit 
    @location = Location.find(params[:id]) 
    end 

    # POST /locations 
    # POST /locations.json 
    def create 
    @location = @user.location.build(params[:location]) 
    respond_to do |format| 
     if @location.save 
     format.html { redirect_to @location, notice: 'Location was successfully created.' } 
     format.json { render json: @location, status: :created, location: @location } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @location.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

답변

0

무엇 당신의 User 모델의 모습입니까? 모델 has_one 또는 belongs_to 다른 모델을 선언 할 때 model.build_association 메서드가 제공됩니다. has_and_belongs_to_many 또는 has_many을 사용하면 model.associations.build이됩니다 (연결된 모델의 복수화에 유의하십시오).

User 모델의 내용에 따라 @user.locations.build 방법 또는 @user.build_location 방법을 사용할 수 있습니다. Rails가 User 모델에 build_location 메소드가 없다는 것에 대해 불평하고 있기 때문에, 나는 당신이 연결이 없다고 가정하고 있습니다.

+0

감사합니다. 그것이 그 것이다. user.locations.build로 변경해야했습니다. – jacobt