Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails插入到模型和关联中。。困惑的_Ruby On Rails_Ruby_Ruby On Rails 4_Model Associations - Fatal编程技术网

Ruby on rails Rails插入到模型和关联中。。困惑的

Ruby on rails Rails插入到模型和关联中。。困惑的,ruby-on-rails,ruby,ruby-on-rails-4,model-associations,Ruby On Rails,Ruby,Ruby On Rails 4,Model Associations,我在为barangay配置文件编写后端时感到困惑。在我的表格中有一个字段,要求输入您的barangay,然后在保存barangay时,barangay字段将保存在barangay模型中。在brgyprofile中,它与barangay模型关联,其中barangay模型的id为brgyprofile模型的FK。如何在barangay模型中保存数据。我已经试过了,但什么也没发生。brgy_的名字没有保存在barangay模型中。我不知道该怎么做。我对rails非常陌生,正在尝试创建一个应用程序 ro

我在为barangay配置文件编写后端时感到困惑。在我的表格中有一个字段,要求输入您的barangay,然后在保存barangay时,barangay字段将保存在barangay模型中。在brgyprofile中,它与barangay模型关联,其中barangay模型的id为brgyprofile模型的FK。如何在barangay模型中保存数据。我已经试过了,但什么也没发生。brgy_的名字没有保存在barangay模型中。我不知道该怎么做。我对rails非常陌生,正在尝试创建一个应用程序

routes.rb

resources :brgyadmins do
    resources :brgyprofiles
  end
模型

class Brgyprofile < ActiveRecord::Base
    belongs_to :barangay
    belongs_to :brgyadmin
end

class Barangay < ActiveRecord::Base
    has_one :brgyprofile
end
_form.html.erb

<%= simple_form_for ([@brgyadmin,@new_profile]) do |f| %>
  <%= f.input :firstname, placeholder: 'Firstname' %>
  <%= f.input :lastname, placeholder: 'Lastname' %>
  <%= f.input :birthdate, :order => [:month, :day, :year] %>
  <%= f.input :gender,
    :as => :radio_buttons,
    :collection => %w[Female Male]
  %>
  <%= f.input :brgy_name, placeholder: 'Barangay' %>
  <%= f.input :brgy_position, placeholder: 'Barangay Position' %>
  <%= f.input :contact_number, placeholder: 'Contact Number' %>
  <%= f.button :submit, 'Submit' %>
<% end %> 
brgyprofile控制器

class BrgyprofilesController < ApplicationController
    before_action :set_brgyprofile, only: [:new, :create]

    def new
        @new_profile = @brgyadmin.build_brgyprofile
    end

    def create
        #respond_to do |format|
            if @brgyadmin.create_brgyprofile(profile_params) && Barangay.create(brgy_name: params[:brgy_name])
                logger.info "Saved!"
            else
                logger.info "Nothing happens"
            end
        #end
    end

    private

    def set_brgyprofile
        @brgyadmin = Brgyadmin.find(session[:user_id])
    end

    def profile_params
        params.require(:brgyprofile).permit(:firstname, :lastname, :birthdate, :gender, :brgy_position, :contact_number, :barangay_id, :brgy_name)
    end

end

你想偷它吗奥特沃克:代码先生D