Ruby on rails 需要简单的数据库和索引帮助
我正在尝试建立一个网站,让棒球教练能够跟踪他们球队的日程安排。我正在尝试制作一个索引,以便教练们能够看到他们未来的所有比赛。然而,奥运会并没有出现。背景和标题仍然存在,只是输入的数据不存在。 这是我的密码: 应用程序控制器Ruby on rails 需要简单的数据库和索引帮助,ruby-on-rails,indexing,Ruby On Rails,Indexing,我正在尝试建立一个网站,让棒球教练能够跟踪他们球队的日程安排。我正在尝试制作一个索引,以便教练们能够看到他们未来的所有比赛。然而,奥运会并没有出现。背景和标题仍然存在,只是输入的数据不存在。 这是我的密码: 应用程序控制器 class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
def show
@schedule = Schedule.find_by_id(params['id'])
end
def create
s = Schedule.new
s.date = params['date']
s.time = params['time']
s.location = params['location']
s.oppo = params['oppo']
s.result = params['result']
redirect_to "/landing"
end
def edit
@schedule = Schedule.find_by_id(params['id'])
end
def update
s = Schedule.find_by_id(params['id'])
s.date = params['date']
s.time = params['time']
s.location = params['location']
s.oppo = params['oppo']
s.result = params['result']
redirect_to "/landing"
end
def destroy
s = Schedule.find_by_id(params['id'])
s.destroy
end
def index
@schedule = Schedule.all
end
end
表格代码
<h2 class="text-center">Add a Game</h2>
<h4 class="text-center">
<form action="/create">
<p>
<input type="text" name="date" placeholder="Date">
</p>
<p>
<input type="text" name="time" placeholder="Time">
<p>
<input type="text" name="location" placeholder="Location">
</p>
<p>
<input type="text" name="oppo" placeholder="Opponent">
</p>
<p>
<input type="text" name="result" placeholder="Score">
</p>
<p>
<input type="submit">
</p>
</h4>
</form>
添加游戏
您正在调用在循环中的控制器中定义的公开的@schedule
集合对象。相反,您应该访问使用每个方法定义的计划
lambda:
<h1> Schedule </h1>
<% @schedule.each do |schedule| %>
<table>
<tr>
<th><h1><%=schedule.date%></h1></th>
<th><%=schedule.time%></th>
<th><%=schedule.location%></th>
<th><%=schedule.oppo%></th>
<th><%=schedule.result%></th>
</tr>
</table>
<% end %>
def create
s = Schedule.new
s.date = params['date']
s.time = params['time']
s.location = params['location']
s.oppo = params['oppo']
s.result = params['result']
if s.save
redirect_to "/landing"
else
# Error of some sort
end
end
那么你的观点是:
<h1> Schedule </h1>
<table>
<% @schedules.each do |schedule| %>
<tr>
<th><h1><%=schedule.date%></h1></th>
<th><%=schedule.time%></th>
<th><%=schedule.location%></th>
<th><%=schedule.oppo%></th>
<th><%=schedule.result%></th>
</tr>
<% end %>
</table>
您是否在计划
模型上有任何默认范围的范围?老实说,我不知道这意味着什么。@M.Overby定义“不工作”。您应该更具描述性,发布更新的代码,可能还有生成的HTML源代码。您的表中确实有数据,对吗?好的,对不起,我是新手。我发现,如果我在rails控制台中手动键入它,它就会工作。所以,我想这是我的表格有问题。这是我表单的代码。@M.Overby我添加了一个编辑来解决您表单的问题
def index
@schedules = Schedule.all
end
<h1> Schedule </h1>
<table>
<% @schedules.each do |schedule| %>
<tr>
<th><h1><%=schedule.date%></h1></th>
<th><%=schedule.time%></th>
<th><%=schedule.location%></th>
<th><%=schedule.oppo%></th>
<th><%=schedule.result%></th>
</tr>
<% end %>
</table>
def create
s = Schedule.new
s.date = params['date']
s.time = params['time']
s.location = params['location']
s.oppo = params['oppo']
s.result = params['result']
if s.save
redirect_to "/landing"
else
# Error of some sort
end
end