【问题标题】:Rails fails to save object information on create methodRails 无法在 create 方法上保存对象信息
【发布时间】: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


    【解决方案1】:

    在您的 def create 中,将 params[offer_params] 替换为 offer_params

    def create
      @listing = Listing.find(params[:id])
      @offer = @listing.offers.build(offer_params) # <- the important change
      if @offer.save
        redirect_to offer_path(:id => @offer.id)
      else
        flash[:error] = @offer.errors.full_messages
        render :new
      end
    end
    

    附加

    .create! -> .build

    • 在创建操作中使用.build 而不是.create!.create! 对您的 if 语句提前调用 .save 导致数据库查询加倍。
    • 另外.create! 不会让错误以静默方式失败;这意味着如果您的 Offer 验证失败,您的应用程序将会中断。

    如果保存失败则渲染:new

    • 如果模型保存失败,通常会重新呈现 :new 视图以防止丢失 :create 模板错误,显示保存错误(即 - “您的报价必须包含价格”)并让用户有机会更正他们的要求。

    【讨论】:

    • 太棒了。这非常有效。非常感谢!
    猜你喜欢
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多