приложение/просмотров/продукты/index.html.erb:Используя другую модель с точки зрения вызывает ошибки тестирования
<p id="notice"><%= notice %></p>
<%= render 'search' %>
<h1>Listing Products</h1>
<br>
<%= will_paginate @products %>
<% if @products.present? %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Image</th>
<th>Price</th>
<th>Category</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @products.each do |product| %>
<tr>
<td><%= product.title %></td>
<td><%= product.description %></td>
<td><%= image_tag product.photo.url(:small) %></td>
<td><%= number_to_currency(product.price, :unit => '$') %></td>
<td><%= link_to Category.find(product.category_id).name, category_path(product.category_id) %></td>
<td><%= link_to 'Show', product %></td>
<% if current_user.try(:admin?) %>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
<td><a href="/cart/<%= product.id %>">Add To Cart</a></td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<% if params[:search].present? %>
<p>There are no products containing the term(s) <b><%= params[:search] %></b>.</p>
<% else %>
<p>There are no any products found in database.</p>
<% end %>
<% end %>
<br>
<% if current_user.try(:admin?) %>
<%= link_to 'New Product', new_product_path %>
<% end %>
приложение/просмотров/продукты/show.html.erb:
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @product.title %>
</p>
<p>
<strong>Description:</strong>
<%= @product.description %>
</p>
<p>
<strong>Image:</strong>
<%= image_tag @product.photo.url(:original) %>
</p>
<p>
<strong>Price:</strong>
<%= @product.price %>
</p>
<p>
<strong>Category:</strong>
<%= @product.category.name %>
</p>
<%= social_share_button_tag(@product.title, :url => request.original_url, :image => root_url + @product.photo.url(:small), desc: @product.description, via: "SCOR") %>
<% if current_user.try(:admin?) %>
<%= link_to 'Edit', edit_product_path(@product) %> |
<% end %>
<%= link_to 'Back', products_path %>
Мои тесты в тест/контроллеры/product_controller_test.rb
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:products)
end
test "should show product" do
get :show, id: @product
assert_response :success
end
Причинение эти ошибки:
1) Error:
ProductsControllerTest#test_should_get_index:
ActionView::Template::Error: Couldn't find Category with 'id'=1
app/views/products/index.html.erb:31:in `block in _app_views_products_index_html_erb___969781135508440478_70267650596940'
app/views/products/index.html.erb:25:in `_app_views_products_index_html_erb___969781135508440478_70267650596940'
test/controllers/products_controller_test.rb:15:in `block in <class:ProductsControllerTest>'
2) Error:
ProductsControllerTest#test_should_show_product:
ActionView::Template::Error: undefined method `name' for nil:NilClass
app/views/products/show.html.erb:25:in `_app_views_products_show_html_erb___1500506684449289207_70267650787820'
test/controllers/products_controller_test.rb:36:in `block in <class:ProductsControllerTest>'
Я бегу моих тестов с rake test:functionals
. Другие тесты для создания, обновления и уничтожения работают, но я получаю ошибки от тестов для индекса и действия show.
Как вы можете видеть, я использую категорию «Категории» в представлении «Продукты» и, очевидно, вызывает эти ошибки. Итак, как я могу решить эту проблему?
Я имею в виду эти строки в представлениях являются проблематичными:
<td><%= link_to Category.find(product.category_id).name, category_path(product.category_id) %></td>
и
<%= @product.category.name %>
Если вы хотите посмотреть на весь проект: https://github.com/mertyildiran/SCOR
Это волшебно решить все, спасибо большое :) –
я Я рад, что смогу помочь тебе – Fred