2011-09-09 2 views
0

나는 내가 이것에 초보자라는 것과 나는 가치가 없다는 것을 알고있다. 그러나 누군가 나를 설명해 주었다. 여기 내가 한 일이있다. 내 기존 레일 애플 리케이션에 내 데이터베이스에 대한 새로운 마이 그 레이션을 생성하고 마이 그 레이션은 "프로필"이라고합니다. db : migrate를 실행 한 다음 이전 "new.html.erb"양식을 편집하기 시작했습니다. 코드는 다음과 같습니다레일 앱에 메소드 없음 오류가 발생하는 이유는 무엇입니까?

class CreateProfiles < ActiveRecord::Migration 
    def self.up 
    create_table :profiles do |t| 
     t.string :major 
     t.string :year 
     t.string :books_sell 
     t.string :books_buy 
     t.string :facebook 
     t.string :restaurants 
     t.string :interests 

     t.timestamps 
    end 
    add_index :profiles, :major 
    add_index :profiles, :year 
    add_index :profiles, :books_sell 
    add_index :profiles, :books_buy 
    add_index :profiles, :facebook 
    add_index :profiles, :restaurants 
    add_index :profiles, :interests 
    end 

    def self.down 
    drop_table :profiles 
    end 
end 

을 기본적으로, 나는 내 응용 프로그램 상에 프로필 섹션을 추가하고,하지만이 무엇입니까 :

undefined method `major' for #<User:0x00000100b6e030> 
    Extracted source (around line #23): 

    20: </div> 
    21: <div class="field"> 
    22:  <%= f.label :"major" %><br /> 
    23:  <%= f.text_field :major %> 
    24: </div> 

이 내 의견/사용자/new.hmtl.erb입니다 파일 :

<h1>Sign up</h1> 

<%= form_for(@user) do |f| %> 
    <%= render 'shared/error_messages', :object => f.object %> 
    <div class="field"> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.text_field :email %> 
    </div> 
    <div class="field"> 
    <%= f.label :password %><br /> 
    <%= f.password_field :password %> 
    </div> 
    <div class="field"> 
    <%= f.label :password_confirmation, "Confirmation" %><br /> 
    <%= f.password_field :password_confirmation %> 
    </div> 
    <div class="field"> 
    <%= f.label :"year" %><br /> 
    <%= f.text_field :year %> 
    </div> 
    <div class="field"> 
    <%= f.label :"major" %><br /> 
    <%= f.text_field :major %> 
    </div> 
    <div class="actions"> 
    <%= f.submit "Sign up" %> 
    </div> 
<% end %> 

무엇이 누락 되었습니까?

+1

귀하의 양식은'사용자'모델을위한 것이고 귀하의 이주는'프로필'모델을위한 것입니다. 이것이 당신 문제일까요? –

+0

@Benoit Garret, 나는 당신이 그 자리에 올랐다 고 생각합니다. 나는 그것을 들여다 볼 것이다. – Tony

+0

진지하게, 초보자 인 것에 대해 사과하지 마십시오. 이 스레드는 초보자 질문에 대한 읽기 : http://meta.stackexchange.com/questions/97327/can-i-ask-newbie-and-basic-questions-in-stack-overflow. –

답변

1

. 나는 그 형식을 택하기를 원했고 그래서 나는 profile이라는 새로운 이주를 만들었다. 수동으로 롤백하여 사용자 모델을 이전 할 수 없었고 문자열과 열을 고정시킬 수 없었기 때문에이 작업을 수행했습니다.

그러나 사용자 모델에서 프로필 모델의 텍스트 필드를 추가하면 오류가 발생합니다.

내가 대신 수행 한 것은 Add_xxx_to_yyy 마이그레이션을 생성하여 이전에 생성 된 마이그레이션에 아무런 문제없이 열을 추가 할 수있었습니다. 내가 레일 3.0에 있기 때문에 나는 rails generate migration Add_profile_to_User을 밑줄과 함께 사용했다 (내가했을 때 Addprofiletouser을했을 때 작동하지 않았다). Et voilà!

+1

'rake db : rollback STEP = n'을 사용하여 마이그레이션을 롤백 할 수 있습니다. 여기서 n은 롤백 할 마이그레이션 수입니다. 롤백 + 마이그레이션을 사용하여 개발할 때 항상 기존 마이그레이션을 수정하고 있습니다. –

+1

마이그레이션 생성의 경우, 낙타의 경우 ('AddProfileToUser') 또는 뱀의 경우 ('add_profile_to_user') 마이그레이션 이름을 지정할 수 있습니다. –

0

Profile 모델의 이전을 붙여 넣었습니다. 그리고 @user 변수에는 User 모델의 새 인스턴스가 있습니다.

그리고 어떤 방법 또는 사용자 정의 속성 major이 없기 때문에, 당신은 불만 볼

"정의되지 않은 방법을 ..."여기서 문제는 내가 이전에 사용자 모델에서 완성 된 양식보기를 한 것이 었습니다

관련 문제