【发布时间】:2015-03-11 17:58:04
【问题描述】:
我在两个模型之间建立了一个简单的有很多关系。 “Listings”可以有多个“Offers”,而Offers只能有一个listing。我可以很好地创建列表,但是当我创建关联的“报价”时 - 我的报价表单中的详细信息没有保存到数据库中。对象本身保存并包含与其关联的列表 ID,但表单中没有任何内容。
我已经粘贴了最相关的代码块,任何建议都将不胜感激。
提供控制器: 类 OffersController
def new
@listing = Listing.find(params[:id])
@offer = @listing.offers.build
end
def create
@listing = Listing.find(params[:id])
@offer = @listing.offers.create!(params[offer_params])
if @offer.save
redirect_to offer_path(:id => @offer.id)
end
end
def show
@offer = Offer.find(params[:id])
end
def index
end
private
def offer_params
params.require(:offer).permit(:offer_price, :offer_terms, :listing_id, :offer_expiration)
end
end
与优惠相关的表单
<%= @listing.street_address %> (<%= link_to "Back", listings_path %>)
<%= form_for [@offer, @listing], :url => { :action => :create, :id => @listing.id}, html: {multipart: true} do |f| %>
<div class="field">
<%= f.label :offer_price %><br>
<%= f.text_field :offer_price %>
</div>
<div class="field">
<%= f.label :offer_terms %><br>
<%= f.text_area :offer_terms %>
</div>
<div class="field">
<%= f.label :offer_expiration %><br>
<%= f.text_field :offer_expiration %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
最后,当我在表单上点击提交时的开发日志条目。如您所见,它将报价详细信息(我认为)正确存储在 params 哈希中,但是当将其插入数据库时,所有报价字段都不会出现。
Started POST "/offers?id=2" for ::1 at 2015-03-11 13:40:01 -0400
Processing by OffersController#create as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"NldTPDo/s/Jyo8SB0I6/OWYWeN0Ukf9JYQU5nASUDONcMl OQTauG+HWxsfjZ4yJLrvDxVtbUaX2sBD63BMIQtA==", "offer"=>{"offer_price"=>"988888", "offer_terms"=>"financed", "offer_expiration"=>"Never"}, "commit"=>"Create Offer", "id"=>"2"}
[1m[35mListing Load (0.2ms)[0m SELECT "listings".* FROM "listings" WHERE "listings"."id" = ? LIMIT 1 [["id", 2]]
[1m[36m (0.1ms)[0m [1m开始交易[0m
[1m[35mSQL (0.4ms)[0m INSERT INTO "offers" ("listing_id", "created_at", "updated_at") 值 (?, ?, ?) [["listing_id", 2], ["created_at", "2015-03-11 17:40:01.967717"], ["updated_at", "2015-03-11 17:40:01.967717"]]
[1m[36m (0.8ms)[0m [1mcommit transaction[0m
[1m[35m (0.1ms)[0m begin transaction
[1m[36m (0.0ms)[0m [1mcommit transaction[0m
Redirected to http://localhost:3000/offers/33
Completed 302 Found in 36ms (ActiveRecord: 2.6ms)
【问题讨论】:
标签: ruby-on-rails-4