【问题标题】:Ruby on Rails | one form - three tables (can't save the form content)Ruby on Rails |一张表格 - 三张表格(无法保存表格内容)
【发布时间】:2018-06-28 09:33:44
【问题描述】:

我正在尝试编写一个应用程序 - 用户详细信息的简单表单,它将数据保存到三个不同的表(1.users、2.companies、3.adresses)。 我使用belongs_to 和has_many 设置了一些关联,并且还使用nested_attributes 和fields_for 来构建一个表单。但我无法保存数据。 我是新手,所以可能我犯了一些愚蠢的错误,但自从 3 天以来我真的找不到它们。还试图在谷歌和这里的论坛帖子中查找,但没有找到代码,问题与子类有关。我在用户、公司和地址之间的联系很奇怪。谁能帮我?

这是我的表格:

<%= simple_form_for @user do |f| %>

    <h5>Personal Details</h5>
      <%= f.input :firstname, label: 'Your First Name:' %>
      <%= f.input :lastname, label: 'Your Last Name:' %>
      <%= f.input :email, label: 'Your Email:' %>
      <%= f.input :dateofbirth, label: 'Your Date of Birth:' %>
      <%= f.input :phonenumber, label: 'Your Phone Number' %>
    <h5>Adress</h5>

      <%= f.fields_for :adresses do |g| %>
          <%= g.input :street, label: 'Street:' %>
          <%= g.input :city, label: 'City:' %>
          <%= g.input :zipcode, label: 'Zip Code:' %>
          <%= g.input :country, label: 'Country:' %>
      <% end %>

     <h5>Company</h5>  
     <%= f.fields_for :companies do |h| %>
          <%= h.input :name, label: 'Name:' %>
     <% end %>      

     <%= f.fields_for :adresses do |i| %>
          <%= i.input :comstreet, label: 'Street:' %>  
          <%= i.input :comcity, label: 'City:' %>  
          <%= i.input :comzipcode, label: 'Zip Code:' %>  
          <%= i.input :comcountry, label: 'Country:' %>  
      <% end %>

      <%= f.button :submit %>
<% end %>

用户.rb

class User < ApplicationRecord
  belongs_to :company, inverse_of: :users
  belongs_to :adress,  inverse_of: :users

  validates_presence_of :firstname, :lastname, :email
  validates_length_of :firstname, :maximum => 100
  validates_length_of :lastname, :maximum => 100
end

公司.rb

class Company < ApplicationRecord
  has_many :users, inverse_of: :companies
  belongs_to :adress, inverse_of: :companies
  accepts_nested_attributes_for :users
end

地址.rb

class Adress < ApplicationRecord
  has_many :users, inverse_of: :adresses
  has_many :companies, inverse_of: :adresses

  accepts_nested_attributes_for :users, :companies
end

users_controller

class UsersController < ApplicationController

  def index
  end

  def new
   @user = User.new 
   @company = Company.new
   @adress = Adress.new
  end

  def create
    @user = User.new(user_params)
    if @user.save 
        redirect_to root_path
    end    
  end    

  private

  def user_params
    params.require(:user).permit(:firstname, :lastname, :email, :dateofbirth, 
    :phonenumber, company_attributes: [:name], 
    adress_attributes: [:street,:city, :zipcode, :country, :comstreet, 
    :comctiy, :comzipcode, :comcountry] )
  end    
end

【问题讨论】:

  • 您有任何错误吗?还要检查您的 user_params ... :comcity 中的拼写是否不正确。
  • 我什至无法保存,所以我无法读取错误列表。但这是我点击提交后控制台中发生的屏幕:gyazo.com/d7515286a8241d95b09553e29406e582

标签: ruby-on-rails ruby simple-form nested-attributes


【解决方案1】:

在您的 user.rb 文件中:

class User < ApplicationRecord
  belongs_to :company, inverse_of: :users
  belongs_to :adress,  inverse_of: :users

  validates_presence_of :firstname, :lastname, :email
  validates_length_of :firstname, :maximum => 100
  validates_length_of :lastname, :maximum => 100
  # add nested attributes here not in other models
  accepts_nested_attributes_for :companies
  accepts_nested_attributes_for :addresses

end

另外,在您的控制器中:仔细检查传递的参数名称。 address_attributes 拼写错误

def user_params
    params.require(:user).permit(:firstname, :lastname, :email, :dateofbirth, 
    :phonenumber, company_attributes: [:name], 
    address_attributes: [:street,:city, :zipcode, :country, :comstreet, 
    :comctiy, :comzipcode, :comcountry] )
  end 

在视图中,删除地址的多个表单元素。你写了两次。

【讨论】:

    【解决方案2】:

    我对代码做了一些改动,可以保存表单,请检查

    这是我的表格:

    <%= simple_form_for @user do |f| %>
    
        <h5>Personal Details</h5>
          <%= f.input :firstname, label: 'Your First Name:' %>
          <%= f.input :lastname, label: 'Your Last Name:' %>
          <%= f.input :email, label: 'Your Email:' %>
          <%= f.input :dateofbirth, label: 'Your Date of Birth:' %>
          <%= f.input :phonenumber, label: 'Your Phone Number' %>
        <h5>Adress</h5>
    
          <%= f.fields_for :adress_attributes do |g| %>
              <%= g.input :street, label: 'Street:' %>
              <%= g.input :city, label: 'City:' %>
              <%= g.input :zipcode, label: 'Zip Code:' %>
              <%= g.input :country, label: 'Country:' %>
          <% end %>
    
         <h5>Company</h5>  
         <%= f.fields_for :company_attributes do |h| %>
              <%= h.input :name, label: 'Name:' %>
                <%= h.fields_for :adress_attributes do |i| %>
                  <%= i.input :street, label: 'Street:' %>  
                  <%= i.input :city, label: 'City:' %>  
                  <%= i.input :zipcode, label: 'Zip Code:' %>  
                  <%= i.input :country, label: 'Country:' %>  
               <% end %>
         <% end %>      
    
    
    
          <%= f.button :submit %>
    <% end %>
    

    用户.rb

    class User < ApplicationRecord
      belongs_to :company, inverse_of: :users
      belongs_to :adress,  inverse_of: :users
    
      validates_presence_of :firstname, :lastname, :email
      validates_length_of :firstname, :maximum => 100
      validates_length_of :lastname, :maximum => 100
      accepts_nested_attributes_for :company
      accepts_nested_attributes_for :address
    end
    

    公司.rb

    class Company < ApplicationRecord
      has_many :users, inverse_of: :companies
      belongs_to :adress, inverse_of: :companies
      accepts_nested_attributes_for :users
      accepts_nested_attributes_for :adress
    end
    

    地址.rb

    class Adress < ApplicationRecord
      has_many :users, inverse_of: :adresses
      has_many :companies, inverse_of: :adresses
    
      accepts_nested_attributes_for :users, :companies
    end
    

    users_controller

    class UsersController < ApplicationController
    
      def index
      end
    
      def new
       @user = User.new 
      end
    
      def create
        @user = User.new(user_params)
        if @user.save 
            redirect_to root_path
        end    
      end    
    
      private
    
      def user_params
        params.require(:user).permit(:firstname, :lastname, :email, :dateofbirth, 
        :phonenumber, company_attributes: [:name, adress_attributes: [:street, 
        :ctiy, :zipcode, :country]], 
        adress_attributes: [:street,:city, :zipcode, :country] )
      end    
    end
    

    它现在正在工作,可以用公司地址保存用户、地址和公司。

    【讨论】:

    • 感谢您的建议!实际上,我仍然无法保存它:(这是来自控制台的错误:gyazo.com/2db0320c2a5b9a9bd1e3785672a157c5
    • @pigeonfield 您缺少公司的 address_attributes 并尝试使用保存检查错误!它会引发异常,因此您将能够找到确切的错误。
    • 你必须在 company_attributes 部分使用 h.adress_attributes。
    猜你喜欢
    • 2014-08-17
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多