2012-01-27 2 views
0

파일을 보관 드라이브로 가져 오거나 이동 한 위치, 이동 한 위치 등 파일을 이동할 때 기록 할 앱을 만듭니다. 나는 내 로컬 레일 서버를 시작하면Rails has_many와 belongs_to association에 무슨 문제가 있습니까?

<table id="archive_table"> 
    <tr> 
    <th>Archive Date</th> 
    <th>Source Directory</th> 
    <th>Archive Directory</th> 
    <th>Archived By ID</th> 
    <th>Archived By Name</th> 
    </tr> 
    <% for archive in @archives %> 
    <tr> 
     <td><%= archive.archive_date %></td> 
     <td><%= archive.start_dir %></td> 
     <td><%= archive.end_dir %></td> 
     <td><%= archive.user_id %></td> 
     <td><%= archive.username %></td> 
     <td><%= link_to "Show", archive %></td> 
     <td><%= link_to "Edit", edit_archive_path(archive) %></td> 
     <td><%= link_to "Destroy", archive, :confirm => 'Are you sure?', :method => :delete %></td> 
    </tr> 
    <% end %> 
</table> 

<p><%= link_to "New Archive", new_archive_path %></p> 

: 이것은 내이다

class User < ActiveRecord::Base 
    # new columns need to be added here to be writable through mass assignment 
    attr_accessible :username, :email, :password, :password_confirmation, :user_id 

    attr_accessor :password 
    before_save :prepare_password 
    before_save :assign_user_id 

    #added by chris 
    has_many :archives 

    def assign_user_id 
    self.user_id = User.maximum('user_id') + 1 
    end 

end 

:

class Archive < ActiveRecord::Base 
    attr_accessible :archive_date, :start_dir, :end_dir, :user_id 

    #added by chris 

    belongs_to :user 
end 

이 내 사용자 모델 :

이 내 보관 모델입니다 via rails s 페이지를로드 해보십시오.

NoMethodError in Archives#index 

Showing c:/appname/app/views/archives/index.html.erb where line #21 raised: 

undefined method `username' for #<Archive:0x203d750> 

Extracted source (around line #21): 

18:  <td><%= archive.start_dir %></td> 
19:  <td><%= archive.end_dir %></td> 
20:  <td><%= archive.user_id %></td> 
21:  <td><%= archive.username %></td> 
22:  <td><%= link_to "Show", archive %></td> 
23:  <td><%= link_to "Edit", edit_archive_path(archive) %></td> 
24:  <td><%= link_to "Destroy", archive, :confirm => 'Are you sure?', :method => :delete %></td> 

Rails.root: c: 
Application Trace | Framework Trace | Full Trace 

app/views/archives/index.html.erb:21:in `block in _app_views_archives_index_html_erb__238412483_16592988' 
app/views/archives/index.html.erb:15:in `each' 
app/views/archives/index.html.erb:15:in `_app_views_archives_index_html_erb__238412483_16592988' 

아카이브 기록이 제대로 내가 어떻게 연결 belongs_to has_many를 통해 사용자의 이름을 표시 내 시야를받을 수 있나요 USER_ID 사용자와 함께 저장됩니다 가정 : 이것은 내가 무엇을 얻을?

내 질문을보고 고맙습니다.

답변

2

아카이브는 사용자가 있으며 사용자는 사용자 이름을가집니다. "높은"레벨로 올라간 것 같습니다. 사용 :

<%= archive.user.username %> 
+0

이것은 완벽하게 작동했습니다. – holaSenor

관련 문제