RubyonRails通过表单向数据库添加对象错误

zzoitvuj  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(323)

我正在尝试在我的web应用程序上创建表单,以便具有正确权限的用户可以向数据库添加新的“站点”。我有一个名为site的数据库表,它在本地计算机上具有所有正确的属性。在表单上单击submit按钮时,出现以下错误。我尝试使用属性访问器,但它导致我以前的许多测试失败。有没有一种不用属性访问器就可以解决这个问题的方法?
错误:
admin::addsitescontroller中的nomethoderror#为nil:nilclass创建未定义的“write_from_user”方法

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

模型

  1. class Site < ApplicationRecord
  2. include ActiveModel::Model
  3. self.table_name = "site"
  4. validates :name, presence: true
  5. validates :address, presence: true
  6. validates :year_built, inclusion: {in: 1700..Date.today.year, message: "that year is fake"},
  7. allow_nil: true
  8. validates :number_of_floors, numericality: {only_integer: true, greater_than: 0}, allow_nil: true
  9. validates :total_floor_area, numericality: {only_integer: true, greater_than: 0}, allow_nil: true
  10. validates :number_of_units, numericality: {only_integer: true, greater_than: 0}, allow_nil: true
  11. validates :primary_use_type, presence: true
  12. validates :number_of_bedrooms, numericality: {only_integer: true, greater_than: 0}, allow_nil: true
  13. validates :people_per_bedroom, numericality: {only_integer: true, greater_than: 0}, allow_nil: true
  14. validates :water_performance_goal, numericality: {only_integer: true, greater_than: 0}, allow_nil: true
  15. belongs_to :organization
  16. belongs_to :weather_station
  17. has_many :site_users, dependent: :destroy
  18. has_many :users, through: :site_users
  19. enum primary_use_type: {
  20. commerical: "Commercial",
  21. residential: "Residential"
  22. }
  23. enum building_type: {
  24. affordable: "Affordable",
  25. market_rate: "Market Rate"
  26. }
  27. enum population_type: {
  28. family: "Family",
  29. elderly_disabled: "Elderly/Disabled",
  30. mixed_other: "Mixed/Other"
  31. }
  32. enum construction_type: {
  33. brick_block: "Brick/Block",
  34. concrete_slab: "Concrete/Slab",
  35. wood_steel: "Light Framed Wood/Steel",
  36. timber_steel: "Heavy Framed Timber/Steel"
  37. }

控制器

  1. module Admin
  2. class AddSitesController < ApplicationController
  3. def new
  4. @site = Site.new
  5. authorize(@site)
  6. end
  7. def create
  8. site = Site.new(site_params)
  9. authorize(site)
  10. if site.valid?
  11. site.save
  12. flash[:notice] = "good"
  13. redirect_to admin_superusers_path
  14. else
  15. flash[:messages] = site.errors.full_messages
  16. redirect_to admin_manage_sites_path
  17. end
  18. end
  19. private
  20. def site_params
  21. params.require(:site).permit(
  22. :organization_id,
  23. :name,
  24. :address,
  25. :year_built,
  26. :construction_type,
  27. :number_of_floors,
  28. :total_floor_area,
  29. :number_of_units,
  30. :weather_station_id,
  31. :primary_use_type,
  32. :building_type,
  33. :population_type,
  34. :number_of_bedrooms,
  35. :people_per_bedroom,
  36. :water_performance_goal,
  37. :sro
  38. )
  39. end
  40. end
  41. end

形式

  1. <%= form_with(
  2. model: @site,
  3. url: admin_add_sites_path,
  4. ) do |form| %>
  5. <div class="pr-8">
  6. <% if flash[:messages] %>
  7. <ul class=“flash alert>
  8. <% flash[:messages].each do |message| %>
  9. <li><%= message %></li>
  10. <% end %>
  11. </ul>
  12. <% end %>
  13. <h2 class="heading-300 text-green-800 mt-3" >
  14. <%= t(".site_info")%>
  15. </h2>
  16. <%= form.label(:organization_id, "Organization", class: "form-label") %>
  17. <% options = options_from_collection_for_select(@organizations, :id, :name, form.object.organization_id) %>
  18. <%= form.select :organization_id, options%>
  19. <%= form.label(:name, "Site name", class: "form-label") %>
  20. <%= form.text_field(:name, autocomplete: "off", autocapitalize: "on", class: "form-input") %>
  21. <%= form.label(:address, class: "form-label") %>
  22. <%= form.text_field(:address, autocomplete: "off", autocapitalize: "on", class: "form-input") %>
  23. <%= form.label("Format: address, city, state, zip code", class: "form-detail text-sm") %>
  24. <h2 class="heading-300 text-green-800 mt-5" >
  25. <%= t(".site_details")%>
  26. </h2>
  27. <%= form.label(:year_built, class: "form-label") %>
  28. <%= form.text_field(:year_built, autocomplete: "off", class: "form-input") %>
  29. <%= form.label(:construction_type, class: "form-label") %>
  30. <%= form.select(:construction_type, Site.construction_types.values, :include_blank => true) %>
  31. <%= form.label(:number_of_floors, class: "form-label") %>
  32. <%= form.text_field(:number_of_floors, autocomplete: "off", class: "form-input") %>
  33. <%= form.label(:total_floor_area, class: "form-label") %>
  34. <%= form.text_field(:total_floor_area, autocomplete: "off", class: "form-input") %>
  35. <%= form.label("Units: sqaure feet", class: "form-detail text-sm") %>
  36. <%= form.label(:number_of_units, class: "form-label") %>
  37. <%= form.text_field(:number_of_units, autocomplete: "off", class: "form-input") %>
  38. <%= form.label(:weather_station_id, "NOAA weather station", class: "form-label") %>
  39. <% options = options_from_collection_for_select(@weather_stations, 'id', 'zip_code', form.object.weather_station_id) %>
  40. <%= form.select :weather_station_id, options%>
  41. <%= form.label(:primary_use_type, class: "form-label") %>
  42. <%= form.select(:primary_use_type, Site.primary_use_types.values, :include_blank => true) %>
  43. <div aria-expanded="false" class="form__drawer">
  44. <div class="pl-8 pb-8 mt-5">
  45. <h2 class="heading-300">
  46. <%= "Residential Options" %>
  47. </h2>
  48. <%= form.label(:building_type, class: "form-label") %>
  49. <%= form.select(:building_type, Site.building_types.values, :include_blank => true) %>
  50. <%= form.label(:population_type, class: "form-label") %>
  51. <%= form.select(:population_type, Site.population_types.values, :include_blank => true) %>
  52. <%= form.label(:number_of_bedrooms, class: "form-label") %>
  53. <%= form.text_field(:number_of_bedrooms, autocomplete: "off", class: "form-input") %>
  54. <%= form.label(:people_per_bedroom, class: "form-label") %>
  55. <%= form.text_field(:people_per_bedroom, class: "form-input") %>
  56. <%= form.label(:water_performance_goal, class: "form-label") %>
  57. <%= form.text_field(:water_performance_goal, autocomplete: "off", class: "form-input", value: 60) %>
  58. <%= form.label("Gal/Bedroom/Day ", class: "form-detail text-sm") %>
  59. <%= form.label(:sro, "SRO", class: "form-label") %>
  60. <%= form.select(:sro, ["yes", "no"], :include_blank => true) %>
  61. </div>
  62. </div>
  63. <div class="mt-2">
  64. <%= form.submit "Add", class: "button-primary w-full" %>
  65. </div>
  66. </div>
  67. <% end %>

路线

  1. namespace :admin do
  2. get "/" => "dashboards#show"
  3. resources :site_users, only: [:update, :destroy]
  4. resources :superusers, only: [:index]
  5. resources :manage_sites, only: [:index, :update, :destroy]
  6. resources :superuser_invitations, only: [:create]
  7. resources :add_sites, only: [:create, :new]
  8. resources :organizations, only: [] do
  9. resources :managers, only: [:index]
  10. resources :manager_invitations, only: [:create]
  11. end
  12. resources :sites, only: [] do
  13. resources :users, only: [:index]
  14. resources :user_invitations, only: [:create]
  15. end
  16. end

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题