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' %>には、パーシャルに移動する行が含まれています。

アプリ/ビュー/製品/_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