2013-05-21 76 views
0

我正在嘗試在用戶頁面上放置一個日曆。我添加了路線到routes.rb文件。但我不明白爲什麼它不起作用。未定義的方法`Calendar'for 2:Fixnum

route.rb

resources :users do 
    resources :calendars 
end 

該錯誤被說成是在calendars_controler.rb21行:

19 def new 
    20  @user = current_user.id 
    21  @calendar = @user.Calendar.new 
    22 end 

application.erb

<h1> <% picture3 = image_tag("../images/twit.png", :border =>0, :class =>"smallh1", :class => "blue")%> </h1> 
<%= link_to picture3, new_user_calendar_path(current_user.id) %> 

型號/ user.rb

has_one :calendar 
    attr_accessible :calendar_id 

我加入了user_id領域日曆指數頁。

型號/ calendar.rb

attr_accessible :event, :published_on, :description, :user_id 
    belongs_to :user, :foreign_key => :user_id 

任何幫助,將不勝感激!

回答

0

更換你的calendars_controller.rb

@calendar = @user.Calendar.new 

@calendar = @user.calendar.new 
0
def new 
    @user = current_user.id 
    @calendar = @user.Calendar.new 
    end 

在這裏,你要分配@user即時變量Fixnum對象(用戶ID)行21,那沒有意義。

@user = current_user 

這是正確的變體。是的,在Ruby中大寫字母是

2
  1. @user變量設置爲用戶的ID,而不是用戶模型實例
  2. 您嘗試類名和模塊名(這是一種特殊類型的類)從@user獲得Calendar,而calendar是正確的字段名稱,其定義爲型號/用戶。RB
  3. 您嘗試從用戶ID得到calendar,而不是從用戶

解決的辦法是:

  1. 更換線

    @user = current_user.id 
    

    @user = current_user 
    
  2. 更換線

    @calendar = @user.Calendar.new 
    

    @calendar = @user.calendar.new 
    
0

我有我的問題,感謝你小子解決

爲解決我做了

高清新 @user = CURRENT_USER @calendar = @ user.build_calendar 結束

當我使用的關聯功能新做不存在它的再次構建現在

感謝

相關問題