【问题标题】:Ruby on Rails - HABTM - Inserting data into 2 models in one controllerRuby on Rails - HABTM - 在一个控制器中将数据插入 2 个模型
【发布时间】:2012-06-15 13:44:59
【问题描述】:

我是 RoR 的初学者,在使用我的一些模型时遇到问题。

基本上,我在产品-机票-预订之间有一种习惯关系。 通过门票预订产品,反之亦然。

我还有一个供应商,它 has_many :products 和 has_many :reservations。

我想做的是在用户选择供应商并看到它的产品之后,他可以从该供应商那里选择他想要的产品。

在该 reservations.new 中,我得到了一个表单,但由于在“提交”操作之后我必须在 2 个模型中插入数据,所以我遇到了问题。

当我创建一个预订时,它应该同时创建一个预订条目和一个票条目,票条目将具有reservation_id和product_id作为外键。

我的预订视图:

<%= form_for(@reservation) do |f| %>

Reservation Info
<div id="reservation_top"></div>
<div id="reservation">

<%= f.label :name %><br />
<%= f.text_field :name %>

<%= f.label :surname %><br />
<%= f.text_field :surname %>            

(...)

<%= f.hidden_field :supplier_id, :value => @reservation.supplier_id %> #to get the supplier ID

Products:

<%= f.fields_for :tickets do |t| %>     
<%= t.select("product_id",options_from_collection_for_select(@products, :id, :name))%>

#I also have another t.select and although this isn't my primary concern, I wanted this t.select option's to change according to what is selected on the previous t.select("product_id"). Something like a postback. How is it done in RoR? I've searched and only found observe_field, but I didn't understand it very much, can you point me in the right direction? thanks

<%end%>

<%= f.label :comments %>
<%= f.text_area :comments %>

<%= f.submit%>

<%end%>

现在我认为问题出在我的控制器上,但我不明白该放什么,我目前有:

 def new
    @supplier=Supplier.find(params[:supplier_id])
    @reservation = Reservation.new(:supplier_id => params[:supplier_id])

    @ticket = Ticket.new(:reservation_id => params[@reservation.id])

    @products = Supplier.find(params[:supplier_id]).products
    @ticket = @reservation.tickets.build

    respond_to do |format|
           format.html 
           format.json { render :json => @reservation }
    end
  end


def create
  @reservation = Reservation.new(params[:reservation])

  respond_to do |format|             
      if @reservation.save
        @reservation.tickets << @ticket

      format.html { redirect_to @reservation, :notice => 'Reservation Successful' }
      else
      format.html { render :action => "new" }
      format.json { render :json => @reservation.errors, :status => :unprocessable_entity }
    end
  end

我现在得到了一个

Called id for nil, which would mistakenly be 4

是不是因为它正在尝试创建一个票证并且它没有reservation_id?

我以前从未处理过 habtm 关联。有什么建议吗?

提前致谢, 问候

【问题讨论】:

    标签: ruby-on-rails activerecord has-and-belongs-to-many


    【解决方案1】:

    查看日志中创建操作的 POST 参数。这将准确地向您显示在保存数据时必须使用 params 中的哪些数据。

    def create
      @reservation = Reservation.new(params[:reservation])
      respond_to do |format|
        if @reservation.save
          @reservation.tickets << @ticket
    

    此时@ticket 是什么? (我相信你的 nil)

    我认为在生成响应之前看看你的@reservation 和@ticket 在你的新方法中的样子可能也很有趣......记录每个对象的 .inspect 以确保你有你的想法你有。

    在像你这样更复杂的保存中,我会将它全部包装在一个事务中。

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      相关资源
      最近更新 更多