问题
im beginner of 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,
<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"
Rails.root: /home/berkay/e-ticaret
SO, how can i save this product?
回答1:
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. So, rails is going to know which brand is parent of that product.
UPDATE
I've created a dummy project which is going to work as you expected.
products/_form.html.erb partial for product
<%= 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
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
<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) %>
回答2:
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:
def new
@brand = Brand.find(params[:brand_id)
@product = @brand.products.build
end
来源:https://stackoverflow.com/questions/59839659/add-a-associated-value-in-ruby-on-rails-from-independent-page