我正在尝试使用表单向产品添加商品。这是我到目前为止所做的。
的routes.rb
resources :products do
collection do
get :showall
end
resources :offers
end
offersController
def new
@offer = Offer.new
end
def create
@product = Product.find(params[:product_id])
@offer = @product.offers.new(offer_params)
@offer.user = current_user
respond_to do |format|
if @offer.save
format.html { redirect_to @offer, notice: 'Offer was successfully created.' }
format.json { render json: @offer, status: :created, location: @offer }
else
format.html { render action: "new" }
format.json { render json: @offer.errors, status: :unprocessable_entity }
end
end
end
private
def set_offer
@offer = Offer.find(params[:id])
end
def offer_params
params.require(:offer).permit(:product_id, :priceOffer, :user_id)
end
end
产品型号
belongs_to :user
has_many :offers
提供型号
belongs_to :user
belongs_to :product
我实际上已尝试通过
制作表格<%= form_for :offer do |f| %>
<div class="field">
<%= f.number_field :priceOffer, :value => @product.price, class:"form-control" %>
</div>
<%= f.submit "Add Offer", class:"btn btn-primary"%>
<% end %>
但是这不起作用。我遇到了这个错误
No route matches [POST] "/products/11"
我尝试用
更改第一行<%= form_for @offer, url:product_offer_path(@product) do |f| %>
但也不起作用:(
答案 0 :(得分:0)
试试
<%= form_for [@product, @offer] do |f| %>