我一直在研究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> </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]
我認爲我的問題是範圍,但我不太熟悉在這個階段的鐵軌。
感謝
非常好。完美的作品。謝謝。我需要等5分鐘才能接受正確的答案。 – ChrisM