2015-04-18 85 views
0

我正在嘗試執行向產品添加報價的表單。這是我迄今爲止所做的。嵌套資源的表單Ror

的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 %> 

做一個形式,但是這並不工作though.I我碰到這樣的錯誤

No route matches [POST] "/products/11" 

我試圖改變第一行以

<%= form_for @offer, url:product_offer_path(@product) do |f| %> 

但是不起作用:(

+0

嘗試accept_nested_attributes ...... http://apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods/accepts_nested_attributes_for – Milind

回答

0

試用

<%= form_for [@product, @offer] do |f| %>