【发布时间】:2021-08-16 01:24:48
【问题描述】:
我正在创建一个简单的市场应用程序,并使用 simple_forms 为用户添加了创建产品列表的选项,这些列表随后显示在 product#index 页面上的表格中,并且可以从那里编辑、删除或查看。
问题是,虽然表单将按要求工作并将我重定向到 product#index 页面,但我添加的输入(描述、名称、价格、类别)这些参数没有显示并在屏幕上保持空白,但我的产品表中的一个插槽已被占用。
请看下面我的控制器代码:
before_action :set_product, only: %i[ :create, :show, :edit, :update, :destroy ]
helper_method :profile
# GET /products or /products.json
def index
@products = Product.all
end
# GET /products/1 or /products/1.json
def show
@product = Product.find_by(params[:product_id])
end
# GET /products/new
def new
@product = Product.new
end
def seller_id
current_user = Seller.id
end
# POST /products or /products.json
def create
@product = Product.create(params[:product_id])
if @product.save!
redirect_to products_path(@product)
end
end
def update
@product = Product.find_by(product_params[:product_id])
if @product.update(product_params)
redirect_to products_path(@product)
end
end
# DELETE /products/1 or /products/1.json
def destroy
@product = Product.delete(params[:id])
redirect_to products_path
end
private
# Use callbacks to share common setup or constraints between actions.
def params_product
@product = Product.find_by(params[:id])
end
# Only allow a list of trusted parameters through.
def product_params
params.permit(:name, :description, :price, :category, :picture, :buyer_id, :seller_id, :product_id)
end
end```
Product.rb (commented out validations as they kept causing errors):
```class Product < ApplicationRecord
belongs_to :buyer, class_name: "Profile", optional: true
belongs_to :seller, class_name: "Profile", optional: true
has_one :picture
# validates_presence_of :name, presence: true
# validates_presence_of :Description, presence: false
# validates_presence_of :Price, presence: true
# validates_presence_of :Category, presence: true
# validates_presence_of :Picture, presence: true
end```
routes:
```Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
resources :products, only: [:show, :new, :destroy, :create, :update, :index, :edit]
resources :pictures, :image_url
devise_for :users
root 'home#page'
get "/products", to: "products#index"
post "/products", to: "products#create"
get "/products/new", to: "products#new"
get "/products/:id/edit", to: "products#edit"
put "/products/:id", to: "products#update"
get "/products/:id", to: "products#show"
delete "/products/:id", to: "products#destroy"
end
视图: -创建
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :Description %>
<%= f.input :Price %>
<%= f.input :Category, collection: ["footwear", "accessories", "menswear", "womenswear"] %>
<%= f.file_field :Picture %>
</div>
<p>
<%= link_to 'Back', products_path %>
</p>
<%end%>
-编辑
<h1 class="heading">Edit Product</h1>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :Description %>
<%= f.input :Price %>
<%= f.input :Category, collection: ["footwear", "accessories", "menswear", "womenswear"] %>
<%= f.file_field :Picture %>
<%= link_to 'Submit', product_path, method: :patch %>
</div>
<p>
<%= link_to 'Back', products_path %>
</p>
<%end%>
编辑:索引 html 文件:
<p id="notice"><%= notice %></p>
<h1>Products</h1>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Category</th>
<th>Seller</th>
<th>Actions</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @products.each do |f| %>
<tr>
<td><%= f.name %></td>
<td><%= f.Description %></td>
<td><%= f.Price %></td>
<td><%= f.Category %></td>
<td><%= f.seller_id %></td>
<td><%= link_to 'Show', product_path(f), method: :get %></td>
<td><%= link_to 'Edit', edit_product_path(f), method: :get %></td>
<td><%= link_to "Delete product", product_path(f), method: :delete, data: { confirm: 'Are you sure you want to delete this product?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Product', new_product_path, method: :get %>
</html>
如果我是 Rails 的初学者,我们将不胜感激。
【问题讨论】:
-
在下面查看我的答案。我已经列出了所有问题。
标签: ruby-on-rails parameters rendering