2015-06-17 37 views
2

Im在使用simple_form(特別是show頁面)使嵌套窗體工作時遇到了一些困難。我有一個配置文件和體驗模型,並希望將體驗模型嵌套在配置文件模型中。我相信我已經正確設置了表單文件和控制器以及關聯,並且表單似乎工作正常,除非當我嘗試保存表單並轉到顯示頁面時,出現與關聯表單相關的以下錯誤以我的經驗模型:Rails simple_form - 「顯示」頁面中的嵌套窗體NoMethodError

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>&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 

提前感謝!

+2

您不要在'show'操作中設置'@ experience'實例變量,這就是爲什麼你會得到這個錯誤。 –

+0

@ profile.experiences use that like –

+0

您如何建議我編寫代碼以在顯示頁面中顯示體驗模型字段的輸入?我試過這種方式,但仍然得到相同的錯誤:@ profile.experiences.company –

回答

1

每個配置文件都有很多經驗,所以當您執行@ 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 %> 
+0

輝煌的工作!非常感謝! –

1

難道你試試這個

<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> 
    <% @profile.experiences.each do |experience| %> 
     <p><strong>Company/Workplace</strong>&nbsp;&nbsp;<%= experience.try(:company) %></p> 
     <p><strong>Company/Workplace</strong>&nbsp;&nbsp;<%= experience.try(:period_of_employment) %></p> 
     <p><strong>Company/Workplace</strong>&nbsp;&nbsp;<%= experience.try(:title) %></p> 
    <% end %> 
    <%= link_to 'Edit', edit_profile_path(@profile) %> | 
    <%= link_to 'Back', profiles_path %> 
</div> 

希望它能幫助!

+0

感謝您的回覆。你的回答有效:) –