1

我将一些代码从我的 RoR 应用程序的用户显示页面移到了根页面,但它不起作用。让我解释 :

首先是我的控制器: app/controllers/users_controllers.rb

class UsersController < ApplicationController
    .
    .
    .
  def show
    @user = User.find(params[:id])
    @owned_products = @user.owned_products.paginate(page: params[:page])
    @borrowed_products = @user.borrowed_products.paginate(page: params[:page])
  end
    .
    .
    .
end

app/views/users/show.html.erb包含<%= render 'products/table' %>驱动到 partial 的行:

应用程序/视图/产品/_table.html.erb

    .
    .
    .
<% if @user.owned_products.any? %>
  <table>
    <% @user.owned_products.each do |owned_product| %>
      <%= render 'products/products', product: owned_product %>
    <% end %>
  </table>
  <%= will_paginate @owned_products %>
<% end %>
    .
    .
    .

哪个驱动到部分: app/views/products/_products.html.erb

<tr>
  <td class="name"><%= product.name %></td>
  <% if product.owner == @user %>
    <td class="availability">
      <% if product.borrower == nil %>
        <%= t('available_since') %>
        <%= time_ago_in_words(product.updated_at) %>
      <% else %>
        <%= t('borrowed_since') %>
        <%= time_ago_in_words(product.updated_at) %>
        <%= t('by') %>
        <%= link_to product.borrower.name, product.borrower %>
      <% end %>
    </td>
  <% else %>
    <td class="availability">
      <%= t('borrowed_since') %>
      <%= time_ago_in_words(product.updated_at) %>
      <%= t('from') %>
      <%= link_to product.owner.name, product.owner %>
    </td>
  <% end %>
</tr>

当我转到用户显示页面时,它工作得很好。

然后我决定把这个表移动到根页面,也就是home/index。

我调整了app/controllers/home_controller.rb

class HomeController < ApplicationController
  def index
    if signed_in?
    @user = current_user
    @product = @user.owned_products.build
    @owned_products = @user.owned_products.paginate(page: params[:page])
    @borrowed_products = @user.borrowed_products.paginate(page: params[:page])
    end
  end
end

我在app/views/home/index.html.erb中有适当的行:

    .
    .
    .
<% if signed_in? %>
  <%= render 'products/table' %>
<% end %>
    .
    .
    .

但随后,沮丧!错误来了:

Showing /home/flo/RoR/letroquet/app/views/products/_products.html.erb where line #7 raised:

undefined method `>' for nil:NilClass

提取的源代码(第 7 行附近):

4  <td class="availability">
5  <% if product.borrower == nil %>
6  <%= t('available_since') %>
7  <%= time_ago_in_words(product.updated_at) %>
8  <% else %>
9  <%= t('borrowed_since') %>
10 <%= time_ago_in_words(product.updated_at) %>

Trace of template inclusion: app/views/products/_table.html.erb, app/views/home/index.html.erb
Rails.root: /home/flo/RoR/letroquet

app/views/products/_products.html.erb:7:in `_app_views_products__products_html_erb__2774185914588219313_69859460726420'
app/views/products/_table.html.erb:10:in `block in _app_views_products__table_html_erb___2883209502501552645_69859460299020'
app/views/products/_table.html.erb:9:in `_app_views_products__table_html_erb___2883209502501552645_69859460299020'
app/views/home/index.html.erb:18:in `_app_views_home_index_html_erb__2755724236080280393_69859462469080'

如果我删除 #7 行,情况会更糟:

/home/flo/RoR/letroquet/app/views/products/_products.html.erb:9: syntax error, unexpected tIDENTIFIER, expecting ')'

...fer.append= ( t('borrowed_since') );@output_buffer.safe_conc...

...                               ^

/home/flo/RoR/letroquet/app/views/products/_products.html.erb:11: syntax error, unexpected tIDENTIFIER, expecting ')'

...;@output_buffer.append= ( t('by') );@output_buffer.safe_conc...

...                               ^

/home/flo/RoR/letroquet/app/views/products/_products.html.erb:21: unknown regexp options - td

unmatched close parenthesis: /td>

');   else 

@output_buffer.safe_concat('    <td class="availability">

      ');@output_buffer.append= ( t('borrowed_since') );@output_buffer.safe_concat('

');@output_buffer.safe_concat('      ');@output_buffer.append= ( time_ago_in_words(product.updated_at) );@output_buffer.safe_concat('

');@output_buffer.safe_concat('      ');@output_buffer.append= ( t('from') );@output_buffer.safe_concat('

');@output_buffer.safe_concat('      ');@output_buffer.append= ( link_to product.owner.name, product.owner );@output_buffer.safe_concat('

');@output_buffer.safe_concat('    </

/home/flo/RoR/letroquet/app/views/products/_products.html.erb:23: unterminated regexp meets end of file

/home/flo/RoR/letroquet/app/views/products/_products.html.erb:23: syntax error, unexpected end-of-input, expecting ')'

提取的源代码(在第 9 行附近):

6  <%= t('available_since') %>
7  <%= # time_ago_in_words(product.updated_at) %>
8  <% else %>
9  <%= t('borrowed_since') %>
10 <%= time_ago_in_words(product.updated_at) %>
11 <%= t('by') %>
12 <%= link_to product.borrower.name, product.borrower %>

Trace of template inclusion: app/views/products/_products.html.erb, app/views/products/_table.html.erb, app/views/home/index.html.erb

Rails.root: /home/flo/RoR/letroquet

app/views/products/_table.html.erb:10:in `block in _app_views_products__table_html_erb___2883209502501552645_69859460299020'
app/views/products/_table.html.erb:9:in `_app_views_products__table_html_erb___2883209502501552645_69859460299020'
app/views/home/index.html.erb:18:in `_app_views_home_index_html_erb__2755724236080280393_69859462469080'

你知道会发生什么吗?

4

0 回答 0