使用rails将同一表单中的多个不同类型的记录更新到表中

时间:2015-02-12 06:37:19

标签: ruby-on-rails

我有2个学生表,地址

create_table "addresses", force: true do |t|
  t.string   "address_line1"
  t.string   "address_line2"
  t.string   "address_line3"
  t.string   "city"
  t.string   "state"
  t.string   "country"
  t.string   "address_type"
  t.integer  "student_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

从同一视图我想更新学生的本地和永久地址,该表在表格中以列address_type区分。 即一名学生在address_type“Permanent”和“Local”的地址表中有两条记录。有可能这样做吗? 我的学生控制器看起来像这样:

class StudentsController < ApplicationController

  def index

    @students = Student.all

  end

 def new

   @student = Student.new

   @addresses = @student.addresses.build

 end

 def create

   @student = Student.new(student_params)
   @addresses = @student.addresses.build(address_params)

   if @student.save &&  @address.save

     redirect_to home_index_path

   else

     redirect_to student_new_path

   end

 end

 private

   def student_params
     params.require(:student).permit(:roll_no)
   end

   def address_params
      params.require(:address).permit(:address_line1, :address_line2 , :address_line3 , :city ,:state, :country, :address_type, :student_id)
   end

请帮我解决这个问题。提前谢谢..

0 个答案:

没有答案