我在Ruby on Rails网站项目中找不到打印源。数组打印在其所显示的表格上方

它看起来像this。正在打印的信息来自下面的表格创建,我只是无法找到如何在不停止打印表格的情况下停止它。

这是相关的代码。

我对项目文件的索引视图

<div class="jumbotron jumbotron-fluid bg-primary">
  <div class="container">
    <h1 class="display-4">Hochschule München Student Projects</h1>
    <p class="lead">This website holds all the student led projects along with information about each of them.</p>
  </div>
</div>

<div class="jumbotron jumbotron-fluid bg-primary">
  <div class="container">
    <h1 class="display-4">Hochschule München Student Projects</h1>
    <p class="lead">This website holds all the student led projects along with information about each of them.</p>
  </div>
</div>

<h1>Listing projects</h1>

<br>

Filter by Professor
<%= collection_select(:project,:project_professor,Project.all.select(:project_professor).uniq,prompt: true) %>

<br> 

<table border="1">
  <tr>
    <th><%= sortable "project_title","Project Title" %></th>
    <th><%= sortable "project_professor","Professor" %></th>
    <th><%= sortable "project_partner","Partner" %></th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

  <%= @projects.each do |project| %>

  <tr>
    <td><%= project.project_title %></td>
    <td><%= project.project_professor%></td>
    <td><%= project.project_partner%></td>
    <td><%= link_to 'Show',project_path(project) %></td>
    <td><%= link_to 'Edit',edit_project_path(project) %></td>
    <td><%= link_to 'Destroy',project_path(project),method: :delete,data: { confirm: 'Are you sure?' } %></td>
  </tr>

  <% end %>
</table>

<br>

<%= link_to 'New project',new_project_path %>

这是控制器文件。

class ProjectsController < ApplicationController

  helper_method :sort_column,:sort_direction

  def index
    @projects = Project.order(sort_column + " " + sort_direction); 0
  end

  def new
    @project = Project.new
  end

  def create
    @project = Project.new(project_params)

    if @project.save
      redirect_to @project
    else
      render 'new'
    end
  end

  def edit
    @project = Project.find(params[:id])
  end

  def update
    @project = Project.find(params[:id])

    if @project.update(project_params)
      redirect_to @project
    else
      render 'edit'
    end
  end

  def show
    @project = Project.find(params[:id])
  end

  def destroy
    @project = Project.find(params[:id])
    @project.destroy

    redirect_to projects_path
  end

  private
    def project_params
      params.require(:project).permit(:project_title,:project_semester,:project_partner,:project_format,:project_department,:project_description,:project_image,:project_link) 
    end

    def sort_column
      Project.column_names.include?(params[:sort]) ? params[:sort] : "project_title"
    def sort_direction
      %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
    end

end

感谢您的帮助!我已经有几天这个问题了,找不到错误。

chenlin5818 回答:我在Ruby on Rails网站项目中找不到打印源。数组打印在其所显示的表格上方

<%= @projects.each do |project| %>



<tr>
    <td><%= project.project_title %></td>
    <td><%= project.project_professor%></td>
    <td><%= project.project_partner%></td>
    <td><%= link_to 'Show',project_path(project) %></td>
    <td><%= link_to 'Edit',edit_project_path(project) %></td>
    <td><%= link_to 'Destroy',project_path(project),method: :delete,data: { confirm: 'Are you sure?' } %></td>
  </tr>

  <% end %>

在Erb中,

尝试像这样编写循环,看看是否有帮助。

<% @projects.each do |project| %>

此处有更多信息:https://guides.rubyonrails.org/action_view_overview.html#erb

,

更改

<%= @projects.each do |project| %>

<% @projects.each do |project| %>

删除=以避免打印each方法的返回值

希望有帮助!

,

更改

<%= collection_select(:project,:project_professor,roject.all.select(:project_professor).uniq,prompt: true) %>

对于

<% collection_select(:project,prompt: true) %>

问题是字符“ =“,因为它告诉Rails应该显示此变量并显示查询带来的结果

本文链接:https://www.f2er.com/3063496.html

大家都在问