需要简单的数据库和索引帮助

时间:2015-08-13 17:42:30

标签: ruby-on-rails indexing

我正在努力为棒球教练建立一个网站,以便能够跟踪他们球队的赛程安排。我正在尝试制作一个索引,以便教练可以看到他们未来的所有比赛。然而,游戏并没有出现。背景和标题仍然存在,而不是输入的数据。 这是我的代码:

应用程序控制器

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

索引

<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 %>`

路由

Rails.application.routes.draw do
 get '/index' => 'application#index'
 get '/landing' => 'application#landing'
 get '/new_game' => 'application#new'
 get '/create' => 'application#create'
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>

1 个答案:

答案 0 :(得分:0)

您正在调用循环中控制器中定义的公开@schedule集合对象。相反,您应该访问使用schedule方法定义的each 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 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>

这是挑剔的,但你的桌面结构有点不稳定。您应该仅将th用于标题单元格,并将td用于内容单元格。 table标记也应该是之外的循环,所以你没有为每个新行声明一个完整的表格,我在第二个例子中解决了这个问题。在整个页面中也应该只有一个h1标签..但这是正确的Web开发的另一个领域。

编辑:您错过了save方法中的create来电:

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