Rails simple_form-“显示”页面中的嵌套表单NoMethodError

肆虐的Al-Julaibi

我在使嵌套表单使用simple_form(特别是在显示页面)上工作时遇到一些困难。我有一个Profile and Experience模型,并且想将Experience模型嵌套在Profile模型中。我相信我已经正确设置了表单文件,控制器和关联,并且该表单似乎可以正常工作,除非当我尝试保存表单并转到显示页面时,关于以下领域,我收到以下错误消息:对我的体验模型:

Profiles#show中的NoMethodError

显示c:/ Users / Rashed / Desktop / Ministry-Web-Application(新)/app/views/profiles/show.html.erb,其中第17行出现:

未定义的方法'company'为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>&nbsp;&nbsp;<%= @profile.name %></p>
        <p><strong>Civil ID no:</strong>&nbsp;&nbsp;<%= @profile.civil %></p>
        <p><strong>Date of Employment:</strong>&nbsp;&nbsp;<%= @profile.date_of_employment %></p>
        <p><strong>Mobile no:</strong>&nbsp;&nbsp;<%= @profile.mobile %></p>
        <p><strong>Work Email:</strong>&nbsp;&nbsp;<%= @profile.work_email %></p>
        <p><strong>Personal Email</strong>&nbsp;&nbsp;<%= @profile.personal_email %></p>
        <p><strong>Internal no:</strong>&nbsp;&nbsp;<%= @profile.internal_no %></p>
        <p><strong>Nationality:</strong>&nbsp;&nbsp;<%= @profile.nationality %></p>
        <p><strong>Gender:</strong>&nbsp;&nbsp;<%= @profile.gender %></p>
        <p><strong>Academic Degree:</strong>&nbsp;&nbsp;<%= @profile.academic_degree %></p>
        <p><strong>Major:</strong>&nbsp;&nbsp;<%= @profile.major %></p>

        <p><strong>Company / Workplace</strong>&nbsp;&nbsp;<%= @experience.company %></p>
        <p><strong>Company / Workplace</strong>&nbsp;&nbsp;<%= @experience.period_of_employment %></p>
    <p><strong>Company / Workplace</strong>&nbsp;&nbsp;<%= @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时,它将返回一个集合,而不是单个记录。您需要遍历这些并像这样显示它们:

<% @profile.experiences.each do |experience| do %>
  <p><strong>Company / Workplace</strong>&nbsp;&nbsp;<%= experience.company %></p>
  <p><strong>Company / Workplace</strong>&nbsp;&nbsp;<%= experience.period_of_employment %></p>
  <p><strong>Company / Workplace</strong>&nbsp;&nbsp;<%= experience.title %></p>
<% end %>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails simple_form-“显示”页面中的嵌套表单NoMethodError

来自分类Dev

用于simple_form的Rails表单关联

来自分类Dev

Rails simple_form嵌套属性

来自分类Dev

使用simple_form Rails的Rich Join Table的嵌套属性

来自分类Dev

如何为嵌套资源使用rails和simple_form?

来自分类Dev

Rails simple_form默认输入值未显示

来自分类Dev

Rails 5中的客户端验证+ simple_form

来自分类Dev

如何在Rails中获取simple_form的动态ID

来自分类Dev

Rails 4中的关联模型和simple_form

来自分类Dev

使用simple_form和rails创建多个嵌套表单4

来自分类Dev

使用Rails / devise / boostrap / simple_form使帮助消息显示在表单字段下方

来自分类Dev

simple_form 中的嵌套表单

来自分类Dev

rails:不使用设计生成具有simple_form的表单

来自分类Dev

具有simple_form和浅层嵌套资源的Rails 4动作路线

来自分类Dev

无法使用Rails simple_form嵌套属性将符号转换为整数错误

来自分类Dev

Rails Devise Simple_form在提交时嵌套了2个模型

来自分类Dev

如何编写简洁的 JS if else 语句以通过 Cocoon 动态添加嵌套的 Rails simple_form?

来自分类Dev

在Ruby on Rails中的simple_form中添加字符计数器

来自分类Dev

在Rails 4中将hstore列与simple_form集成

来自分类Dev

在rails 3.2.12中的simple_form视图中单击“提交”按钮时,是否可以传递参数?

来自分类Dev

使用simple_form在Rails中自定义日期时间字段

来自分类Dev

Ruby on Rails-在simple_form gem中添加人为安全字段?

来自分类Dev

Rails 4-URL中的simple_form和预填充字段

来自分类Dev

如何在Rails 3上使用simple_form在选择选项中强制选择值

来自分类Dev

在Rails 4中将hstore列与simple_form集成

来自分类Dev

在Rails中通过simple_form选择框-保存ID,而不是标题

来自分类Dev

如何将类添加到 Rails 中的 simple_form 输入包装器?

来自分类Dev

Rails simple_form记住密码输入

来自分类Dev

红宝石,simple_form Rails,集合

Related 相关文章

  1. 1

    Rails simple_form-“显示”页面中的嵌套表单NoMethodError

  2. 2

    用于simple_form的Rails表单关联

  3. 3

    Rails simple_form嵌套属性

  4. 4

    使用simple_form Rails的Rich Join Table的嵌套属性

  5. 5

    如何为嵌套资源使用rails和simple_form?

  6. 6

    Rails simple_form默认输入值未显示

  7. 7

    Rails 5中的客户端验证+ simple_form

  8. 8

    如何在Rails中获取simple_form的动态ID

  9. 9

    Rails 4中的关联模型和simple_form

  10. 10

    使用simple_form和rails创建多个嵌套表单4

  11. 11

    使用Rails / devise / boostrap / simple_form使帮助消息显示在表单字段下方

  12. 12

    simple_form 中的嵌套表单

  13. 13

    rails:不使用设计生成具有simple_form的表单

  14. 14

    具有simple_form和浅层嵌套资源的Rails 4动作路线

  15. 15

    无法使用Rails simple_form嵌套属性将符号转换为整数错误

  16. 16

    Rails Devise Simple_form在提交时嵌套了2个模型

  17. 17

    如何编写简洁的 JS if else 语句以通过 Cocoon 动态添加嵌套的 Rails simple_form?

  18. 18

    在Ruby on Rails中的simple_form中添加字符计数器

  19. 19

    在Rails 4中将hstore列与simple_form集成

  20. 20

    在rails 3.2.12中的simple_form视图中单击“提交”按钮时,是否可以传递参数?

  21. 21

    使用simple_form在Rails中自定义日期时间字段

  22. 22

    Ruby on Rails-在simple_form gem中添加人为安全字段?

  23. 23

    Rails 4-URL中的simple_form和预填充字段

  24. 24

    如何在Rails 3上使用simple_form在选择选项中强制选择值

  25. 25

    在Rails 4中将hstore列与simple_form集成

  26. 26

    在Rails中通过simple_form选择框-保存ID,而不是标题

  27. 27

    如何将类添加到 Rails 中的 simple_form 输入包装器?

  28. 28

    Rails simple_form记住密码输入

  29. 29

    红宝石,simple_form Rails,集合

热门标签

归档