【问题标题】:Rails Simple Form input not rendering on products#index or #show pagesRails 简单表单输入未在 products#index 或 #show 页面上呈现
【发布时间】: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


【解决方案1】:

问题 1:

在你的product_controllerdef create

您使用@product = Product.create(params[:product_id]) 创建了产品。

应该是@product = Product.create(product_params)

问题 2:

您的代码中有太多不一致的地方。在产品_form 中,您使用了Description(上D),但在控制器product_params 中您允许description(下D)。区分大小写是个问题。不建议使用驼峰式表格列。保持小写。

问题 3:

您允许参数:params.permit(:name, :description....)

错了!

正确方法:params.require(:product).permit(:name, :description....)

【讨论】:

  • 不幸的是,我已经尝试过了,它仍然会导致表单被提交,但他们的输入没有像上面那样显示..还有什么你能看到的可能导致这种情况的原因吗?
  • 在您的产品列表视图文件中可能有问题。当您提交用于创建新产品的表单时,您可以编辑您的答案并发布视图文件和请求日志。
  • @SnehaBhamra 您的代码有太多不一致之处。在产品_form 中,您使用了Description,但在控制器product_params 中您允许description。区分大小写是个问题。不建议使用驼峰式表格列。保持小写。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 2014-04-09
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
相关资源
最近更新 更多