rails嵌套表单问题

时间:2017-10-12 00:03:57

标签: ruby-on-rails ruby

我试图让嵌套表单显示空字段(显示当前现有数据)。解决了这一部分,看到更新。

此外,我试图找到一种方法一次编辑一个嵌套的表单条目,目前我只能编辑以在编辑时在同一表单上显示所有嵌套的表单条目。理想情况下,我希望每个职位旁边都有一个链接,只能编辑一个条目,而不是在编辑模式下打开的所有条目。

用户控制器

# renders the users/new.html.erb form
def new
  @user = User.new
  @user.work_histories.build # used for nested attributes in forms
end


# renders the users/edit.html.erb view form
def edit
  @user = User.find(params[:id])
  @user.work_histories.build # used for nested forms
end

# white listed form attributes for user and also for nested attributes (work history, qualifications, etc...). 
 def user_params
   params.require(:user).permit(:first_name, :last_name, :email, :phone, :current_job_title, :password, :password_confirmation, 
                  work_histories_attributes: [:user_id, :id, :job_title, :company, :description, :city, :state,:start_date, :end_date])
 end   

用户模型

      # allows for nested attributes in the user views.
  accepts_nested_attributes_for :work_histories, allow_destroy: true, 
  reject_if: lambda {|attributes| attributes['job_title'].blank?}

WorkHistory模型

  belongs_to :user, optional: true

用户的节目视图。模态显示first_name的用户字段,但不显示工作历史字段。

<!-- modal -->
<div class="modal fade" id="experienceModal" tabindex="-1" role="dialog" aria-labelledby="experienceModal" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">

      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times fa-fw" aria-hidden="true"></i></button>
        <h4 class="modal-title" id="myModalLabel">Experience</h4>
      </div>

      <div class="modal-body">

      <!-- one-to-many nested attributes -->  
      <%= form_for(@user) do |f| %>

            <%= f.label :first_name %>
            <%= f.text_field :first_name, size: 40, autofocus: true, class: 'form-control' %>

        <%= f.fields_for :work_histories do |ff| %>

            <%= ff.label :job_title %>
            <%= ff.text_field :job_title, size: 40, autofocus: true, class: 'form-control' %>

        <% end %><!-- fields_for -->

        <%= f.submit 'Save', class: 'btn btn-primary' %>

      </div><!-- modal body -->

        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>

      <% end %><!-- form_for -->  

    </div><!-- modal-content -->
  </div><!-- modal dialog -->
</div><!-- modal -->

更新 好吧我想出了如何让表单显示为空,这样我就可以让用户填写数据,而不是使用现有用户数据显示的表单。只需在视图中添加WorkHistory.new。

 <%= form_for @user do |user_form| %>

  <%= user_form.label :first_name %>
  <%= user_form.text_field :first_name, autofocus: true, class: "form-control" %>

  <%= user_form.fields_for :work_histories, WorkHistory.new do |wh_fields| %>
      job: <%= wh_fields.text_field :job_title %>
  <% end %>

<% end %>

1 个答案:

答案 0 :(得分:0)

更新此内容以防将来有人偶然发现它。要获取要显示的空白嵌套表单以便用户可以输入数据,请参阅我原始问题中名为UPDATE的部分。

要实际编辑现有的嵌套表单记录,请参阅下文,必须同时传递用户ID和工作历史记录ID。

    <table>
  <!-- one-to-many association to loop through a users qualifications -->
  <% @user.work_histories.each do |w| %>        
<tr>
  <td>        
    <h4><b><%= link_to w.job_title, edit_user_work_history_path(user_id: @user.id, id: w.id) %></b></h4>
    <!--<h4><b><%= link_to w.job_title, edit_user_work_history_path(user_id: @user.id, id: w.id), data: {toggle: "modal", target: "#experienceModal"} %></b></h4>-->
  </td>
</tr>
<tr>
  <td>
    <%= w.company %>
  </td>
</tr>
<tr>
  <td>
    <%= w.start_date.strftime("%b %Y") %> - <%= w.end_date.strftime("%b %Y") %>
  </td>
</tr>  
<tr>
  <td>
    <%= w.city %>, <%= w.state %>
  </td>
</tr> 
<tr>
  <td>
    <br />
    <%= w.description %>
  </td>
</tr>  
<tr>
  <td>
    <br />
    <hr>
  </td>
</tr>      
 <% end %>