无法在一页上显示2个部分

时间:2019-05-28 19:41:38

标签: ruby-on-rails

我正在尝试执行“简单的待办事项列表”任务,但是我不知道如何将其显示在屏幕截图http://joxi.net/v29DKEVcBLB5mG上的页面上(也就是说,有一个已保存项目列表,右边是添加新表单的表单)。如果所有内容都是单独的,它将为我工作,但是当它在一页上呈现2部分时-错误undefined method each for #<Track:0x00007f8fc42c2698> (NoMethodError in Tracks#new) 这是我的new.html.erb

<h1> Create a New Item!</h1>
<%= render 'table' %>
<%= render 'form' %>

表部分:

<table border="1">
  <thead>
    <tr>
      <th>ID</th>
      <th>All Tracks</th>
      <th>It's description</th>
      <th>Priority</th>
      <th>Date</th>
      <th>Delete</th>
      <th>Restore</th>
    </tr>
  </thead>
  <tbody>
    <% @track.each do |track| %>
    <tr>
      <td> <%= track.id %> </td>
      <td> <%= link_to track.item, edit_track_path(track.id) %> </td>
      <td> <%= track.description %> </td>
      <td> <%= track.priority %> </td>
      <td> <%= track.created_at.strftime('%d/%m/%Y') %> </td>
      <td><%= button_to "Delete", track_path(track.id), method: :delete %></td>
      <td><%= button_to "Restore", track_path(track.id), method: :put %></td>
    </tr>
    <% end %>
  </tbody>
</table>

和_form部分:

  <%= form_for @track do |f| %>
  <p>
    <%= f.label :item %><br>
    <%= f.text_field :item %>
  </p>
  <p>
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </p>
  <p>
    <%= f.select :priority, priorities,
        prompt: "Select priority" %>
  </p>
  <p>
    <%= f.submit %>
  </p>
  <% end %>
    <%= link_to 'Back', tracks_path %>
    <%= link_to 'Home', root_path %>

并跟踪控制器:

class TracksController < ApplicationController
  before_action :find_track, only: [:show, :edit, :update, :destroy]

  def index
    @track = Track.all
  end

  def show
  end

  def new
    @track = Track.new
  end

  def create
    @track = Track.new(track_params)
    if @track.save
      flash[:success] = "It works!"
      redirect_to tracks_path
    else
      flash[:success] = "Its wrong!"
      render 'new'
    end
  end

  def update
    if @track.update(track_params)
      redirect_to @track
    else
      render 'edit'
    end
  end

  def edit
  end

  def destroy
    @track.destroy
    redirect_to tracks_path
  end

private
  def track_params
    params.require(:track).permit(:item, :description, :priority)
  end

  def find_track
    @track = Track.find(params[:id])
  end
end

谢谢!

2 个答案:

答案 0 :(得分:1)

在控制器中,尝试将 @tracks 用于多个记录。

  def index
    @tracks = Track.all
  end

在表中部分使用以下内容

<table border="1">
  <thead>
    <tr>
      <th>ID</th>
      <th>All Tracks</th>
      <th>It's description</th>
      <th>Priority</th>
      <th>Date</th>
      <th>Delete</th>
      <th>Restore</th>
    </tr>
  </thead>
  <tbody>
    <% @tracks.each do |track| %>
    <tr>
      <td> <%= track.id %> </td>
      <td> <%= link_to track.item, edit_track_path(track.id) %> </td>
      <td> <%= track.description %> </td>
      <td> <%= track.priority %> </td>
      <td> <%= track.created_at.strftime('%d/%m/%Y') %> </td>
      <td><%= button_to "Delete", track_path(track.id), method: :delete %></td>
      <td><%= button_to "Restore", track_path(track.id), method: :put %></td>
    </tr>
    <% end %>
  </tbody>
</table>

您已将@track用于单个和多个记录。尝试将其 @tracks 用于多个记录,将 @track 用于单个对象。

答案 1 :(得分:1)

自从呈现Tracks#new动作的页面以来,它应该是

def new
  @tracks = Track.all
  @track = Track.new
end

<% @tracks.each do |track| %>

在视图迭代器中。另外,您需要将@tracks = Track.all添加到create动作中,否则将由于验证错误而无法渲染轨迹表。

请注意,按照惯例,我们对集合使用from复数,对单个记录使用single。您需要在@tracks操作中同时包含@tracknew,因为您要呈现所有页面并创建一个新页面。

顺便说一句,您在任何地方都使用index操作吗?如果不是,则可以安全地将其从控制器和routes.rb

中删除