Ruby on Rails 通过表单错误向数据库添加对象

如何解决Ruby on Rails 通过表单错误向数据库添加对象

我正在尝试在我的网络应用程序上创建一个表单,以便具有正确权限的用户可以向数据库添加一个新的“站点”。我有一个名为 site 的数据库表,在我的本地机器上具有所有正确的属性。单击表单上的提交按钮时,出现以下错误。我尝试使用属性访问器,但它导致我以前的许多测试失败。有没有办法不使用属性访问器来解决这个问题?

错误

Admin::AddSitesController#create 中的 NoMethodError nil:NilClass 的未定义方法 `write_from_user'

 def create
      site = Site.new(site_params) <-- highlighted in error message
      authorize(site)

模型

class Site < ApplicationRecord
  include ActiveModel::Model
  self.table_name = "site"

  validates :name,presence: true
  validates :address,presence: true
  validates :year_built,inclusion: {in: 1700..Date.today.year,message: "that year is fake"},allow_nil: true
  validates :number_of_floors,numericality: {only_integer: true,greater_than: 0},allow_nil: true
  validates :total_floor_area,allow_nil: true
  validates :number_of_units,allow_nil: true
  validates :primary_use_type,presence: true
  validates :number_of_bedrooms,allow_nil: true
  validates :people_per_bedroom,allow_nil: true
  validates :water_performance_goal,allow_nil: true

  belongs_to :organization
  belongs_to :weather_station
  has_many :site_users,dependent: :destroy
  has_many :users,through: :site_users

  enum primary_use_type: {
    commerical: "Commercial",residential: "Residential"
  }
  enum building_type: {
    affordable: "Affordable",market_rate: "Market Rate"
  }
  enum population_type: {
    family: "Family",elderly_disabled: "Elderly/disabled",mixed_other: "Mixed/Other"
  }
  enum construction_type: {
    brick_block: "Brick/Block",concrete_slab: "Concrete/Slab",wood_steel: "Light Framed Wood/Steel",timber_steel: "Heavy Framed Timber/Steel"
  }

控制器

module Admin
  class AddSitesController < ApplicationController
    def new
      @site = Site.new

      authorize(@site)
    end

    def create
      site = Site.new(site_params)

      authorize(site)

      if site.valid?
        site.save
        flash[:notice] = "good"
        redirect_to admin_superusers_path
      else
        flash[:messages] = site.errors.full_messages
        redirect_to admin_manage_sites_path
      end
    end

    private

    def site_params
      params.require(:site).permit(
        :organization_id,:name,:address,:year_built,:construction_type,:number_of_floors,:total_floor_area,:number_of_units,:weather_station_id,:primary_use_type,:building_type,:population_type,:number_of_bedrooms,:people_per_bedroom,:water_performance_goal,:sro
      )
    end
  end
end

表格

<%= form_with(
    model: @site,url: admin_add_sites_path,) do |form| %>
    <div class="pr-8">
  
      <% if flash[:messages] %>
        <ul class=“flash alert”>
          <% flash[:messages].each do |message| %>
          <li><%= message %></li>
           <% end %>
          </ul>
      <% end %>
  
      
      <h2 class="heading-300 text-green-800 mt-3" >
      <%= t(".site_info")%>
      </h2>
  
  
      <%= form.label(:organization_id,"Organization",class: "form-label") %>
      <% options = options_from_collection_for_select(@organizations,:id,form.object.organization_id) %>
      <%= form.select :organization_id,options%>
  
  
      <%= form.label(:name,"Site name",class: "form-label") %>
      <%= form.text_field(:name,autocomplete: "off",autocapitalize: "on",class: "form-input") %>
  
      <%= form.label(:address,class: "form-label") %>
      <%= form.text_field(:address,class: "form-input") %>
      <%= form.label("Format: address,city,state,zip code",class: "form-detail text-sm") %>
  
      <h2 class="heading-300 text-green-800 mt-5" >
      <%= t(".site_details")%>
      </h2>
  
      <%= form.label(:year_built,class: "form-label") %>
      <%= form.text_field(:year_built,class: "form-input") %>
  
      <%= form.label(:construction_type,class: "form-label") %>
      <%= form.select(:construction_type,Site.construction_types.values,:include_blank => true) %>
  
      <%= form.label(:number_of_floors,class: "form-label") %>
      <%= form.text_field(:number_of_floors,class: "form-input") %>
  
      <%= form.label(:total_floor_area,class: "form-label") %>
      <%= form.text_field(:total_floor_area,class: "form-input") %>
      <%= form.label("Units: sqaure feet",class: "form-detail text-sm") %>
  
      <%= form.label(:number_of_units,class: "form-label") %>
      <%= form.text_field(:number_of_units,class: "form-input") %>
  
      <%= form.label(:weather_station_id,"NOAA weather station",class: "form-label") %>
      <% options = options_from_collection_for_select(@weather_stations,'id','zip_code',form.object.weather_station_id) %>
      <%= form.select :weather_station_id,options%>
     

  
      <%= form.label(:primary_use_type,class: "form-label") %>
      <%= form.select(:primary_use_type,Site.primary_use_types.values,:include_blank => true) %>
  
  
  
    
  
      <div aria-expanded="false" class="form__drawer">
        <div class="pl-8 pb-8 mt-5">
  
          <h2 class="heading-300">
            <%= "Residential Options" %>
          </h2>
  
          <%= form.label(:building_type,class: "form-label") %>
          <%= form.select(:building_type,Site.building_types.values,:include_blank => true) %>
  
          <%= form.label(:population_type,class: "form-label") %>
          <%= form.select(:population_type,Site.population_types.values,:include_blank => true) %>
  
          <%= form.label(:number_of_bedrooms,class: "form-label") %>
          <%= form.text_field(:number_of_bedrooms,class: "form-input") %>
  
          <%= form.label(:people_per_bedroom,class: "form-label") %>
          <%= form.text_field(:people_per_bedroom,class: "form-input") %>
  
          <%= form.label(:water_performance_goal,class: "form-label") %>
          <%= form.text_field(:water_performance_goal,class: "form-input",value: 60) %>
          <%= form.label("gal/bedroom/Day ",class: "form-detail text-sm") %>

  
          <%= form.label(:sro,"SRO",class: "form-label") %>
          <%= form.select(:sro,["yes","no"],:include_blank => true) %>
        </div>
      </div>
  
      <div class="mt-2">
      <%= form.submit "Add",class: "button-primary w-full" %>
    </div>
    
    </div>

  <% end %>

路线

  namespace :admin do
    get "/" => "dashboards#show"
    resources :site_users,only: [:update,:destroy]
    resources :superusers,only: [:index]
    resources :manage_sites,only: [:index,:update,:destroy]
    resources :superuser_invitations,only: [:create]
    resources :add_sites,only: [:create,:new]
    resources :organizations,only: [] do
      resources :managers,only: [:index]
      resources :manager_invitations,only: [:create]
    end
    resources :sites,only: [] do
      resources :users,only: [:index]
      resources :user_invitations,only: [:create]
    end
  end

解决方法

想通了 - 只需要删除

include ActiveModel::Model

来自站点模型,因为它已经通过其表连接到数据库。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?