将代码从用户页面移动到根页面

时间:2013-05-17 09:20:19

标签: ruby-on-rails null

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

首先是我的控制器: 应用/控制器/ 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' %>行:

应用/视图/产品/ _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 %>
    .
    .
    .

驱动到部分: 应用/视图/产品/ _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'

你知道会发生什么事吗?

0 个答案:

没有答案