【问题标题】:Form for nested resource Ror嵌套资源 Ror 的表单
【发布时间】:2015-04-18 20:23:55
【问题描述】:

我正在尝试制作一个表格来为产品添加报价。这是我到目前为止所做的。

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

但效果不佳:(

【问题讨论】:

标签: ruby-on-rails ruby submit form-for


【解决方案1】:

试一试

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多