Rails simple_form - "show" 页面中的嵌套表单 NoMethodError
Rails simple_form - Nested Form NoMethodError in "show" page
我在使用 simple_form 制作嵌套表单时遇到了一些困难,特别是在显示页面上。我有一个 Profile 和 Experience 模型,并且想将 Experience 模型嵌套在 Profile 模型中。我相信我已经正确设置了表单文件和控制器以及关联,并且表单似乎工作正常,除了当我尝试保存表单并转到显示页面时,我收到以下与相关字段相关的错误我的体验模型:
NoMethodError in Profiles#show
Showing c:/Users/Rashed/Desktop/Ministry-Web-Application
(new)/app/views/profiles/show.html.erb where line #17 raised:
undefined method `company' for nil:NilClass
问题似乎与我在显示页面上呈现嵌套体验字段的代码有关,但我似乎无法弄清楚如何解决这个问题,尽管我已经寻找了很长时间的解决方案。
这是我的项目的代码:
show.html.erb:
<div class="row">
<div class="col-md-offset-1 col-md-8">
<p id="notice"><%= notice %></p>
<p><strong>Full Name:</strong> <%= @profile.name %></p>
<p><strong>Civil ID no:</strong> <%= @profile.civil %></p>
<p><strong>Date of Employment:</strong> <%= @profile.date_of_employment %></p>
<p><strong>Mobile no:</strong> <%= @profile.mobile %></p>
<p><strong>Work Email:</strong> <%= @profile.work_email %></p>
<p><strong>Personal Email</strong> <%= @profile.personal_email %></p>
<p><strong>Internal no:</strong> <%= @profile.internal_no %></p>
<p><strong>Nationality:</strong> <%= @profile.nationality %></p>
<p><strong>Gender:</strong> <%= @profile.gender %></p>
<p><strong>Academic Degree:</strong> <%= @profile.academic_degree %></p>
<p><strong>Major:</strong> <%= @profile.major %></p>
<p><strong>Company / Workplace</strong> <%= @experience.company %></p>
<p><strong>Company / Workplace</strong> <%= @experience.period_of_employment %></p>
<p><strong>Company / Workplace</strong> <%= @experience.title %></p>
<%= link_to 'Edit', edit_profile_path(@profile) %> |
<%= link_to 'Back', profiles_path %>
</div>
</div>
profiles_controller.rb
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
@profiles = Profile.all
respond_with(@profiles)
end
def show
respond_with(@profile)
end
def new
@profile = Profile.new
@profile.experiences.build
respond_with(@profile)
end
def edit
end
def create
@profile = Profile.new(profile_params)
@profile.user_id = current_user.id
@profile.save
respond_with(@profile)
end
def update
@profile.update(profile_params)
respond_with(@profile)
end
def destroy
@profile.destroy
respond_with(@profile)
end
private
def set_profile
@profile = Profile.find(params[:id])
end
def profile_params
params.require(:profile).permit(:name, :civil, :date_of_employment, :mobile, :work_email, :personal_email, :internal_no, :nationality, :gender, :academic_degree, :major, :work_experience, experiences_attributes: [:company, :period_of_employment, :title])
end
end
_form.html.erb
<%= simple_form_for(@profile) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name, label: 'Full Name:' %>
<%= f.input :civil, label: 'Civil ID:' %>
<%= f.input :gender, collection: ['Male', 'Female'], label: 'Gender:', as: :radio_buttons, :include_blank => false %>
<%= f.input :date_of_employment, label: 'Date of Employement:', as: :date, start_year: Date.today.year - 50, end_year: Date.today.year, order: [:day, :month, :year], input_html: { class: 'inline-date' } %>
<%= f.input :mobile, label: 'Mobile no:' %>
<%= f.input :work_email, label: 'Work Email:' %>
<%= f.input :personal_email, label: 'Personal Email:' %>
<%= f.input :internal_no, label: 'Internal no:' %>
<%= f.input :nationality, collection: ['Kuwaiti', 'Non-Kuwaiti'], label: 'Nationality:', as: :radio_buttons, :include_blank => false %>
<%= f.input :academic_degree, collection: ["Pre-Bachelor's Degree", "Diploma", "Bachelor's Degree", "Master's Degree", "Ph.D"], label: 'Academic Degree', as: :select, :include_blank => 'Choose Your Degree...' %>
<%= f.input :major, collection: ["Architecture", "Civil Engineering", "Mechanical Engineering", "Chemical Engineering", "Computer Engineering", "Interior Designer", "Electrical Engineering", "Civil Engineering / Structure", "Administration"], label: 'Major', as: :select, :include_blank => 'Choose Your Major...' %>
<%= f.input :nationality, collection: ['Kuwaiti', 'Non-Kuwaiti'], label: 'Nationality:', as: :radio_buttons, :include_blank => false %>
<%= f.input :work_experience, collection: ['No', 'Yes'], label: 'Previous Work Experience?', as: :radio_buttons, :include_blank => false %>
<%= f.simple_fields_for :experiences, Experience.new do |builder| %>
<%= buidler.input :company %>
<%= buidler.input :period_of_employment, label: 'Period of Employement:', as: :date, start_year: Date.today.year - 50, end_year: Date.today.year, order: [:day, :month, :year], input_html: { class: 'inline-date' } %>
<%= builder.input :title, label: 'Job Title' %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
体验模型:
class Experience < ActiveRecord::Base
belongs_to :profile
end
资料模型:
class Profile < ActiveRecord::Base
belongs_to :users
has_many :experiences
accepts_nested_attributes_for :experiences
end
提前致谢!
每个个人资料都有很多经验,所以当您执行@profile.experiences时,它returns是一个集合,而不是一条记录。 您需要迭代这些并像这样显示它们:
<% @profile.experiences.each do |experience| do %>
<p><strong>Company / Workplace</strong> <%= experience.company %></p>
<p><strong>Company / Workplace</strong> <%= experience.period_of_employment %></p>
<p><strong>Company / Workplace</strong> <%= experience.title %></p>
<% end %>
你能试试这个吗
<div class="row">
<div class="col-md-offset-1 col-md-8">
<p id="notice"><%= notice %></p>
<p><strong>Full Name:</strong> <%= @profile.name %></p>
<p><strong>Civil ID no:</strong> <%= @profile.civil %></p>
<p><strong>Date of Employment:</strong> <%= @profile.date_of_employment %></p>
<p><strong>Mobile no:</strong> <%= @profile.mobile %></p>
<p><strong>Work Email:</strong> <%= @profile.work_email %></p>
<p><strong>Personal Email</strong> <%= @profile.personal_email %></p>
<p><strong>Internal no:</strong> <%= @profile.internal_no %></p>
<p><strong>Nationality:</strong> <%= @profile.nationality %></p>
<p><strong>Gender:</strong> <%= @profile.gender %></p>
<p><strong>Academic Degree:</strong> <%= @profile.academic_degree %></p>
<p><strong>Major:</strong> <%= @profile.major %></p>
<% @profile.experiences.each do |experience| %>
<p><strong>Company / Workplace</strong> <%= experience.try(:company) %></p>
<p><strong>Company / Workplace</strong> <%= experience.try(:period_of_employment) %></p>
<p><strong>Company / Workplace</strong> <%= experience.try(:title) %></p>
<% end %>
<%= link_to 'Edit', edit_profile_path(@profile) %> |
<%= link_to 'Back', profiles_path %>
</div>
希望对您有所帮助!
我在使用 simple_form 制作嵌套表单时遇到了一些困难,特别是在显示页面上。我有一个 Profile 和 Experience 模型,并且想将 Experience 模型嵌套在 Profile 模型中。我相信我已经正确设置了表单文件和控制器以及关联,并且表单似乎工作正常,除了当我尝试保存表单并转到显示页面时,我收到以下与相关字段相关的错误我的体验模型:
NoMethodError in Profiles#show
Showing c:/Users/Rashed/Desktop/Ministry-Web-Application (new)/app/views/profiles/show.html.erb where line #17 raised:
undefined method `company' for nil:NilClass
问题似乎与我在显示页面上呈现嵌套体验字段的代码有关,但我似乎无法弄清楚如何解决这个问题,尽管我已经寻找了很长时间的解决方案。
这是我的项目的代码:
show.html.erb:
<div class="row">
<div class="col-md-offset-1 col-md-8">
<p id="notice"><%= notice %></p>
<p><strong>Full Name:</strong> <%= @profile.name %></p>
<p><strong>Civil ID no:</strong> <%= @profile.civil %></p>
<p><strong>Date of Employment:</strong> <%= @profile.date_of_employment %></p>
<p><strong>Mobile no:</strong> <%= @profile.mobile %></p>
<p><strong>Work Email:</strong> <%= @profile.work_email %></p>
<p><strong>Personal Email</strong> <%= @profile.personal_email %></p>
<p><strong>Internal no:</strong> <%= @profile.internal_no %></p>
<p><strong>Nationality:</strong> <%= @profile.nationality %></p>
<p><strong>Gender:</strong> <%= @profile.gender %></p>
<p><strong>Academic Degree:</strong> <%= @profile.academic_degree %></p>
<p><strong>Major:</strong> <%= @profile.major %></p>
<p><strong>Company / Workplace</strong> <%= @experience.company %></p>
<p><strong>Company / Workplace</strong> <%= @experience.period_of_employment %></p>
<p><strong>Company / Workplace</strong> <%= @experience.title %></p>
<%= link_to 'Edit', edit_profile_path(@profile) %> |
<%= link_to 'Back', profiles_path %>
</div>
</div>
profiles_controller.rb
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
@profiles = Profile.all
respond_with(@profiles)
end
def show
respond_with(@profile)
end
def new
@profile = Profile.new
@profile.experiences.build
respond_with(@profile)
end
def edit
end
def create
@profile = Profile.new(profile_params)
@profile.user_id = current_user.id
@profile.save
respond_with(@profile)
end
def update
@profile.update(profile_params)
respond_with(@profile)
end
def destroy
@profile.destroy
respond_with(@profile)
end
private
def set_profile
@profile = Profile.find(params[:id])
end
def profile_params
params.require(:profile).permit(:name, :civil, :date_of_employment, :mobile, :work_email, :personal_email, :internal_no, :nationality, :gender, :academic_degree, :major, :work_experience, experiences_attributes: [:company, :period_of_employment, :title])
end
end
_form.html.erb
<%= simple_form_for(@profile) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name, label: 'Full Name:' %>
<%= f.input :civil, label: 'Civil ID:' %>
<%= f.input :gender, collection: ['Male', 'Female'], label: 'Gender:', as: :radio_buttons, :include_blank => false %>
<%= f.input :date_of_employment, label: 'Date of Employement:', as: :date, start_year: Date.today.year - 50, end_year: Date.today.year, order: [:day, :month, :year], input_html: { class: 'inline-date' } %>
<%= f.input :mobile, label: 'Mobile no:' %>
<%= f.input :work_email, label: 'Work Email:' %>
<%= f.input :personal_email, label: 'Personal Email:' %>
<%= f.input :internal_no, label: 'Internal no:' %>
<%= f.input :nationality, collection: ['Kuwaiti', 'Non-Kuwaiti'], label: 'Nationality:', as: :radio_buttons, :include_blank => false %>
<%= f.input :academic_degree, collection: ["Pre-Bachelor's Degree", "Diploma", "Bachelor's Degree", "Master's Degree", "Ph.D"], label: 'Academic Degree', as: :select, :include_blank => 'Choose Your Degree...' %>
<%= f.input :major, collection: ["Architecture", "Civil Engineering", "Mechanical Engineering", "Chemical Engineering", "Computer Engineering", "Interior Designer", "Electrical Engineering", "Civil Engineering / Structure", "Administration"], label: 'Major', as: :select, :include_blank => 'Choose Your Major...' %>
<%= f.input :nationality, collection: ['Kuwaiti', 'Non-Kuwaiti'], label: 'Nationality:', as: :radio_buttons, :include_blank => false %>
<%= f.input :work_experience, collection: ['No', 'Yes'], label: 'Previous Work Experience?', as: :radio_buttons, :include_blank => false %>
<%= f.simple_fields_for :experiences, Experience.new do |builder| %>
<%= buidler.input :company %>
<%= buidler.input :period_of_employment, label: 'Period of Employement:', as: :date, start_year: Date.today.year - 50, end_year: Date.today.year, order: [:day, :month, :year], input_html: { class: 'inline-date' } %>
<%= builder.input :title, label: 'Job Title' %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
体验模型:
class Experience < ActiveRecord::Base
belongs_to :profile
end
资料模型:
class Profile < ActiveRecord::Base
belongs_to :users
has_many :experiences
accepts_nested_attributes_for :experiences
end
提前致谢!
每个个人资料都有很多经验,所以当您执行@profile.experiences时,它returns是一个集合,而不是一条记录。 您需要迭代这些并像这样显示它们:
<% @profile.experiences.each do |experience| do %>
<p><strong>Company / Workplace</strong> <%= experience.company %></p>
<p><strong>Company / Workplace</strong> <%= experience.period_of_employment %></p>
<p><strong>Company / Workplace</strong> <%= experience.title %></p>
<% end %>
你能试试这个吗
<div class="row">
<div class="col-md-offset-1 col-md-8">
<p id="notice"><%= notice %></p>
<p><strong>Full Name:</strong> <%= @profile.name %></p>
<p><strong>Civil ID no:</strong> <%= @profile.civil %></p>
<p><strong>Date of Employment:</strong> <%= @profile.date_of_employment %></p>
<p><strong>Mobile no:</strong> <%= @profile.mobile %></p>
<p><strong>Work Email:</strong> <%= @profile.work_email %></p>
<p><strong>Personal Email</strong> <%= @profile.personal_email %></p>
<p><strong>Internal no:</strong> <%= @profile.internal_no %></p>
<p><strong>Nationality:</strong> <%= @profile.nationality %></p>
<p><strong>Gender:</strong> <%= @profile.gender %></p>
<p><strong>Academic Degree:</strong> <%= @profile.academic_degree %></p>
<p><strong>Major:</strong> <%= @profile.major %></p>
<% @profile.experiences.each do |experience| %>
<p><strong>Company / Workplace</strong> <%= experience.try(:company) %></p>
<p><strong>Company / Workplace</strong> <%= experience.try(:period_of_employment) %></p>
<p><strong>Company / Workplace</strong> <%= experience.try(:title) %></p>
<% end %>
<%= link_to 'Edit', edit_profile_path(@profile) %> |
<%= link_to 'Back', profiles_path %>
</div>
希望对您有所帮助!