2015-10-30 163 views
0

我一直在研究Rails項目。只開始學習來自Java背景的軌道Rails - 未定義的局部變量或方法`時間表'

我創建了一個時間表模型和控制器,用戶可以將他們的時間表上傳到網站。

的錯誤我得到它

undefined local variable or method `timetable' for #<#<Class:0x007fcde39dcbd8>:0x007fcde38b0ea8> 

時間表控制器

class TimetablesController < ApplicationController 

    def index 
    @timetables = Timetable.all 
    end 

    def new 
    @timetable = Timetable.new 
    end 

    def create 
    @timetable = Timetable.new(timetable_params) 

    if @timetable.save 
     redirect_to timetable_path, notice: "The resume #{@timetable.name} has been uploaded." 
    else 
     render "new" 
    end 
    end 

    def destroy 
    @timetable = Timetable.find(params[:id]) 
    @timetable.destroy 
    redirect_to timetables_path, notice: "The timetable #{@timetable.name} has been deleted." 
    end 

    private 
    def timetable_params 
    params.require(:timetable).permit(:name, :attachment) 
    end 
end 

時間表指數

<% if !flash[:notice].blank? %> 
    <div class="alert alert-info"> 
     <%= flash[:notice] %> 
    </div> 
<% end %> 
<br /> 
<%= link_to "New Timetable", new_timetable_path, class: "btn btn-primary" %> 
<br /> 
<br /> 
<table class="table table-bordered table-striped"> 
    <thead> 
    <tr> 
    <th>Name</th> 
    <th>Download Link</th> 
    <th>&nbsp;</th> 
    </tr> 
    </thead> 
    <tbody> 
    <% @timetables.each do |resume| %> 
     <tr> 
     <td><%= timetable.name %></td> 
     <td><%= link_to "Download Resume", timetable.attachment_url %></td> 
     <td><%= button_to "Delete", timetable, method: :delete, class: "btn btn-danger", confirm: "Are you sure that you wish to delete #{timetable.name}?" %></td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

Relavent路由條目

resources :timetables, only: [:index, :new, :create, :destroy] 

我認爲我的問題是範圍,但我不太熟悉在這個階段的鐵軌。

感謝

回答

1

在你的索引操作定義你的時間表,收集像這樣:

@timetables = Timetable.all 

要使用在你看來,你通過你的時間表命名每一個迭代resume該實例變量。我想你想它是時間表:

<% @timetables.each do |timetable| %> 
    <tr> 
    <td><%= timetable.name %></td> 
    <td><%= link_to "Download Resume", timetable.attachment_url %></td> 
    <td><%= button_to "Delete", timetable, method: :delete, class: "btn btn-danger", confirm: "Are you sure that you wish to delete #{timetable.name}?" %></td> 
    </tr> 
<% end %> 
+0

非常好。完美的作品。謝謝。我需要等5分鐘才能接受正確的答案。 – ChrisM

0

您必須更改恢復成時間表,因爲目前還沒有時間表變量。

<% @timetables.each do |timetable| %> 
     <tr> 
     <td><%= timetable.name %></td> 
     <td><%= link_to "Download Resume", timetable.attachment_url %></td> 
     <td><%= button_to "Delete", timetable, method: :delete, class: "btn btn-danger", confirm: "Are you sure that you wish to delete #{timetable.name}?" %></td> 
     </tr> 
<% end %> 
+0

我的一個疏忽,但簡歷只在字符串中提到。 – ChrisM

+0

其實對不起我的錯誤。我現在看到它。謝謝 – ChrisM

+0

我的意思是循環中的「resume」,但沒關係。我希望它能幫助你。 – akbarbin

相關問題