需要使用fields_for将复选框值保存到rails4中的其他表。

时间:2016-09-30 07:49:21

标签: ruby-on-rails ruby ruby-on-rails-4

这是视图页面.---这里使用方法的字段我将数据从check_box传递给JobPosting控制器,复选框数据需要保存在JobAssignment表中。

<%- if @leads.present? -%>
              <%= f.fields_for :assign do |x| %>
                 <%- @leads.each do |client| -%>
                 <% @requiter=UserRecruiter.where("reporting_to = ?", client.id)%>
                      <div class="col-md-3">                 
                      <table class="table table-bordered table-striped ">
                        <thead >
                            <tr  class="bg-primary">
                                <th>
                                <%= x.check_box :assign_to, {}, client.id %>

                          <%= "#{client.first_name}  #{client.last_name}" %>
                                </th>
                            </tr>
                        </thead>

                        <tbody  style="height:110px">

                          <%- if @requiter.present? -%>
                            <%- @requiter.each do |req| -%>
                            <tr>
                              <td>
                                <%= "#{req.first_name}  #{req.last_name}" %>
                                </td>
                                </tr>
                                <%end%>
                                <%end%>
                        </tbody>       
                      </table>  </div>
                      <%end%><%end%>
                      <%end%>
控制器中的

---

def params_value
      params.require(:job_posting).permit(:title, :description,:assign [])
end

def add_or_update_users
    if params[:job_posting][:assign].present? && params[:job_posting]      [:assign][:assign_to].present?  
   assign = params[:job_posting][:assign][:assign_to]  
   assign.each do |x|
       @job.job_assignment = JobAssignment.where({ job_posting_id:      @job.id, user_id: x }).first_or_create
       @job.save
   end
   end
end

数据没有保存到JobAssignment表。任何人都告诉我我在哪里做错了???

1 个答案:

答案 0 :(得分:0)

您可以使用build

def new
  @jobpos = JobPosting.new
  @jobpos.jobassignment.build
end

并将:assign替换为:jobassignment