html - rails 给出 activerecord 而不是 id
问题描述
当 id do/products/1
它显示我的产品 < 这是正确的,但是当我点击它显示的链接时products/%23<Product::ActiveRecord_Relation:0x00007f11426994c0>
,这对于我点击的每个链接都是不同的。现在的问题是当我点击链接时它说它Couldn't find Product with 'id'=#<Product::ActiveRecord_Relation:0x00007f11426994c0>
应该不做 id 而不是 ActiveRecord ???
这是我的产品控制器
class ProductsController < ApplicationController
include ProductsHelper
# before_action :logged_in?, only: [:index, :show, :edit]
before_action :admin_only, only: %i[destroy reset_votes]
def index
@products = Product.all
end
def edit
@product = Product.find(params[:id])
end
def create
@product_params = params.require(:product).permit(%i[name size image])
end
def destroy
end
def add_to_cart
id = params[:id].to_i
if session[:cart].include?(id)
session[:cart].delete(id)
else
session[:cart] << id
end
redirect_back(fallback_location: root_path)
end
def show
@products = Product.find(params[:id])
end
def update
@product = Product.find(params[:id])
product_params = params.require(%i[name size image])
@product.update_attributes(product_params)
end
private
def product_params
params.require(:category).permit(%i[name size image])
end
end
这是我的产品索引,如图一所示
<table>
<tr>
<th>Name</th>
<th>Size</th>
<th>Price</th>
<th>Category</th>
<th>Image</th>
</tr>
<% Product.all.each do |f| %>
<tr>
<td><%= link_to f.name, product_path(@products) %></td>
<td><%= f.size %></td>
<td><%= f.price %></td>
<td><%= image_tag(f.image, :width => "60%") %></td>
<% end %>
</tr>
</tr>
</table>
我遇到的另一个问题是,当我访问products/1/edit
并填写表格时,它给出了另一个问题,说No route matches [PUT] "/products/1/edit"
在我的路线中,我已将其作为resources :products
这是产品/edit.html.erb
<% provide(:title, "Edit Product - ") %>
<h1>Edit Product <%= @products %></h1>
<p class="lead">Fill out the following fields</p>
<%= render 'products/form', products: @products, url: products_path(@products), method: :put %>
这是产品/_form.html.erb
<div class="form">
<%= form_for :products, method: method do |f| %>
<%= render 'shared/flash_messages' %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control', placeholder: 'Product Name' %>
</div>
<div class="form-group">
<%= f.label :size %>
<%= f.text_field :size, class: 'form-control', placeholder: 'Product Size' %>
</div>
<div class="form-group">
<%= f.label :price %>
<%= f.text_field :price, class: 'form-control', placeholder: 'Product Size' %>
</div>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image, hidden: true %>
</div>
</div>
<%= f.submit class: 'btn btn-primary btn-primary--wide', value: 'Update Product' %>
<% end %>
</div>
提前谢谢你!
解决方案
好像你混淆了变量的形式singular
!plural
在您的产品>索引文件中,更改以下行:
<td><%= link_to f.name, product_path(@products) %></td>
至:
<td><%= link_to f.name, product_path(f) %></td>
为什么会发生这种变化?您正在运行一个循环,@products
并且您正在发送product_path
这个@products
变量,这是一个 AR 记录列表!f
您应该只发送一个您在循环中定义的产品对象。[这是一个非常糟糕的变量命名]
另一件事:
表格中的相同问题update
:
<%= render 'products/form', products: @products, url: products_path(@products), method: :put %>
在这里你应该通过@product
,不是@products
!您正在更新一条记录,而不是产品列表,对吗?
推荐阅读
- python - 交换和分配最小和最大元素的麻烦
- swift - UIRefreshControl 在启用 preferLargeTitles 的情况下向下滚动时出现奇怪的跳转
- sql - 多个 Where 子句不起作用
- mysql - 在每日销售数据库中添加包含昨天销售信息的列
- error-handling - PlatformException 抛出意外
- c++ - 在实现 (.cpp) 文件中不包括相应的头 (.h) 文件仍然可以编译吗?
- javascript - 模型预测 0 值?
- powershell - 无法将格式化的 Get-ADOrganizationlUnit 输出传递到脚本中的函数
- python-3.7 - smtplib.SMTPNotSupportedError:服务器不支持 SMTP AUTH 扩展
- node.js - 在 Jest 的不同环境中运行相同的测试