2015-04-18 6 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

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

ответ

Смежные вопросы