Ruby on Rails Saving in two tables from one form

后端 未结 3 836
失恋的感觉
失恋的感觉 2020-12-10 09:43

I have two models Hotel and Address. Relationships are:

class Hotel
  belongs_to :user
  has_one    :address
  accepts_nested_attributes_for :address
         


        
相关标签:
3条回答
  • 2020-12-10 10:11

    You are using wrong method for appending your child with the parent.And also it is has_one relation,so you should use build_model not model.build.Your new and create methods should be like this

    class HotelsController < ApplicationController
      def new
        @hotel = Hotel.new
        @hotel.build_address #here
      end
    
      def create
        @hotel = current_user.hotels.build(hotel_params)
    
        if @hotel.save      
          flash[:success] = "Hotel created!"
          redirect_to @hotel
        else
          render 'new'      
        end    
      end
    

    Update

    Your hotel_params method should look like this

    def hotel_params
       params.require(:hotel).permit(:title, :stars, :room, :price,address_attributes: [:country,:state,:city,:street])
    end
    
    0 讨论(0)
  • 2020-12-10 10:20

    You should not build address again

    class HotelsController < ApplicationController
      def new
        @hotel = Hotel.new
      end
    
      def create
        @hotel = current_user.hotels.build(hotel_params)
        # address = @hotel.address.build 
        # the previous line should not be used
        if @hotel.save      
          flash[:success] = "Hotel created!"
          redirect_to @hotel
        else
          render 'new'      
        end    
      end
    
    0 讨论(0)
  • 2020-12-10 10:22

    Bottom line here is you need to use the f.fields_for method correctly.

    --

    Controller

    There are several things you need to do to get the method to work. Firstly, you need to build the associated object, then you need to be able to pass the data in the right way to your model:

    #app/models/hotel.rb
    Class Hotel < ActiveRecord::Base
       has_one :address
       accepts_nested_attributes_for :address
    end
    
    #app/controllers/hotels_controller.rb
    Class HotelsController < ApplicationController
        def new
           @hotel = Hotel.new
           @hotel.build_address #-> build_singular for singular assoc. plural.build for plural
        end
    
        def create
            @hotel = Hotel.new(hotel_params)
            @hotel.save
        end
    
        private
    
        def hotel_params
            params.require(:hotel).permit(:title, :stars, :room, :price, address_attributes: [:each, :address, :attribute])
        end
    end
    

    This should work for you.

    --

    Form

    Some tips for your form - if you're loading the form & not seeing the f.fields_for block showing, it basically means you've not set your ActiveRecord Model correctly (in the new action)

    What I've written above (which is very similar to that written by Pavan) should get it working for you

    0 讨论(0)
提交回复
热议问题