简体   繁体   中英

Ruby on Rails - HABTM - Inserting data into 2 models in one controller

I'm a beginner in RoR and am having issues on working with some of my models.

Basically I have a habtm relation between a product-ticket-reservation. A product habtm reservations through tickets and vice-versa.

I also have a Supplier, which has_many :products and has_many :reservations.

What I want to do is after the user selects a supplier and sees it's products, he may then select the products he wants from that supplier.

In that reservations.new I got a form but since after the "submit" action I have to insert data in 2 models, I'm having issues with it.

When I create a reservation, it is supposed to create a reservation entry and a ticket entry at the same time, the ticket entry will have the reservation_id and the product_id as foreign keys.

My Reservations' view:

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

Now i think the problem is in my controller, but I can't understand what to put there, I currently have:

 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

I'm now getting a

Called id for nil, which would mistakenly be 4

Is it because it is trying to create a ticket and it doesn't have the reservation_id?

I've never handled habtm associations before. Any tips?

Thanks in advance, Regards

Take a look at the POST params for your create action in your log. That will show you exactly what data you have to work with from params when it comes time to save your data.

In

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

what is @ticket at that point? (There's your nil I believe)

I think it might also be interesting to see what your @reservation and @ticket look like in your new method right before generating the response... log a .inspect of each of those objects to make sure you have what you think you have.

And in a more complicated save like you have, I'd wrap it all in a transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM