首页 > 解决方案 > 从独立页面在 ruby​​ on rails 中添加关联值

问题描述

我是 ruby​​ on rails 的初学者。我有品牌 - 产品列表。

牌,

class Brand < ApplicationRecord
  has_many :products, dependent: :destroy
  validates :title, presence: true,
                    length: { minimum: 2 }
end

产品,

class Product < ApplicationRecord
  belongs_to :brand
end

产品.控制器

class ProductsController < ApplicationController
   skip_before_action :verify_authenticity_token


  def edit
     @product = Product.find(params[:id])
  end

  def new
    @product = Product.new
  end

  def update
    @brand = Brand.find(params[:brand_id])
    @product = Product.find(params[:id])
    @product.update(product_params)
    redirect_to brand_path(@brand)
  end

  def create
    @brand = Brand.find(params[:brand_id])
    @product = @brand.products.create(product_params)
    redirect_to brand_path(@brand)
  end

  def destroy
    @brand = Brand.find(params[:brand_id])
    @product = @brand.products.find(params[:id])
    @product.destroy
    redirect_to brand_path(@brand)
  end

  def update
    @brand = Brand.find(params[:brand_id])
    @product = @brand.products.find(params[:id])
    @product.destroy
  end

  helper_method :update

  private
    def product_params
      params.require(:product).permit(:name)
    end



end

新的.html.erb,

    <h1> Add a new product </h1>

<%= form_with model: @brand, local: true do |form| %>

<p>
  <%= form.label :title,"Product name" %><br>
  <%= form.text_field :name %>
</p>


  <p>
    <%= form.label :title,"Select a Brand" %><br>
    <%= form.collection_select(:brand, Brand.all, :id, :title) { @brand = Brand.find(params[:brand_id]) } %>

  </p>

  <p>
    <%= form.submit "Save Product", :onclick => "create" %>
  </p>

<% end %>

路线.rb

Rails.application.routes.draw do
  get 'welcome/index'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  resources :brands do
    resources :products
  end
  root 'welcome#index'
end

当我单击按钮时出现错误,没有路线匹配 [POST] "/brands/%23%3CBrand::ActiveRecord_Relation:0x00007f5e30d87490%3E/products/new"

Rails.root:/home/berkay/e-ticaret

那么,我该如何保存这个产品?

标签: ruby-on-railsruby

解决方案


改变那个

<%= form_with model: @brand, local: true do |form| %>

<%= form_with(model: @product, url: [@brand, @product]) %>

还添加

@brand = Brand.find(a_brand_id)

在你new的课堂方法中ProductsController。因此,rails 将知道哪个品牌是该产品的父品牌。

更新

我创建了一个虚拟项目,它将按您的预期工作。

products/_form.html.erb部分产品

<%= form_with(model: product, local: true) do |form| %>
  <% if product.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2>

      <ul>
        <% product.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :title %>
    <%= form.text_field :title %>
  </div>

  <div class="field">

    <%= form.label :title, "Select a Brand" %><br>
    <%= form.collection_select(:brand_id, Brand.all, :id, :title, {selected: @brand.id})  %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

路线.rb

Rails.application.routes.draw do
  resources :products
  resources :brands
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

products_controller.rb

class ProductsController < ApplicationController


  # GET /products/new
  def new
    if params[:brand_id]
      @brand = Brand.find(params[:brand_id])
    end
    @product = Product.new
  end

  def edit
    @brand = @product.brand
  end 

   ...

    # Never trust parameters from the scary internet, only allow the white list through.
  def product_params
    params.require(:product).permit(:title, :brand_id)
  end
end

我还添加了一个热链接来为给定品牌创建产品

品牌/show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <strong>Title:</strong>
  <%= @brand.title %>
</p>

<%= link_to 'Edit', edit_brand_path(@brand) %> |
<%= link_to 'Back', brands_path %> |
<%= link_to 'Create Products', new_product_path(brand_id: @brand.id) %>

品牌页面

创建产品 输入并提交 成功


推荐阅读