简体   繁体   English

从独立页面在 ruby​​ on rails 中添加关联值

[英]add a associated value in ruby on rails from independent page

im beginner of ruby on rails.我是 ruby​​ on rails 的初学者。 i have brand - products list.我有品牌 - 产品列表。

Brand,牌,

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

Product,产品,

class Product < ApplicationRecord
  belongs_to :brand
end

products.controller产品.控制器

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

the new.html.erb, new.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 %>

routes.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

when i cliked the button i get error, No route matches [POST] "/brands/%23%3CBrand::ActiveRecord_Relation:0x00007f5e30d87490%3E/products/new"当我点击按钮时出现错误,没有路由匹配 [POST] "/brands/%23%3CBrand::ActiveRecord_Relation:0x00007f5e30d87490%3E/products/new"

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

SO, how can i save this product?所以,我该如何保存这个产品?

change that one改变那个

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

to

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

Also add还添加

@brand = Brand.find(a_brand_id)

inside your new method of ProductsController class.ProductsController类的new方法中。 So, rails is going to know which brand is parent of that product.因此,rails 将知道哪个品牌是该产品的母公司。

UPDATE更新

I've created a dummy project which is going to work as you expected.我创建了一个虚拟项目,它将按您的预期工作。

products/_form.html.erb partial for product 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 %>

routes.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 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

I've also added a hotlink to create a product for a given brand我还添加了一个热链接来为给定品牌创建产品

brands/show.html.erb品牌/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) %>

品牌页面

创建产品 输入并提交 成功

When using nested routes you would have to write使用嵌套路由时,您必须编写

 form_with model: [@brand, @product]

it will use the array to compose the path and the last item will be actually edited.它将使用数组来组成路径,最后一个项目将被实际编辑。

Your new action should best be changed as follows:您的new操作最好更改如下:

def new 
  @brand = Brand.find(params[:brand_id)
  @product = @brand.products.build  
end 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM