ruby-on-rails - 如何在 Rails 中使用一种表单创建另一个模型时更新模型

标签 ruby-on-rails nested-forms ruby-on-rails-4.2

请我在这里需要帮助。 我有 2 个模型:- due_job 和 Outgoing_job due_job 有_许多传出_jobs 传出工作属于到期工作。 我试图更新用户的传出作业,同时为另一个用户创建到期作业。 我的模型:

class DueJob < ActiveRecord::Base
    belongs_to :user
    has_many :outgoing_jobs
    accepts_nested_attributes_for :outgoing_jobs
end

class OutgoingJob < ActiveRecord::Base
    belongs_to :user
    belongs_to :outgoing_jobs
    accepts_nested_attributes_for :outgoing_jobs
end

Controller :

class OutgoingJobsController < ApplicationController

    def index
        @outgoing_job = OutgoingJob.new
        @outgoing_jobs = OutgoingJob.all
    end

    def new 
        @outgoing_job = OutgoingJob.new 
    end 

    def create
        @outgoing_job = OutgoingJob.new(outgoing_job_params)
        respond_to do |format|
        
            if @outgoing_job.save   
                flash.now[:success] = "saved"
                format.html {redirect_to current_user}
                format.json {render json: @outgoing_job, status: :created, location: @outgoing_job}
            else 
                flash[:danger] = "not saved"
                format.html {redirect_to root_path}
                format.json {render json: @outgoing_job.errors, status: :unprocessable_entity }
            end
        end
    end

    def show
        @outgoing_job = OutgoingJob.find(params[:id])
    end 

    def update 
        @outgoing_job = OutgoingJob.find(params[:id])
        respond_to do |format|
            if @outgoing_job.update(outgoing_job_params)
                format.html { redirect_to '/users/outgoing_job_dashboard', notice: 'job updated' }
                format.json {render action: 'show', status: :ok, location: @outgoing_job }
            else
                format.html { render action: 'edit'}
                format.json { render json: @outgoing_job.errors, status: :unprocessable_entity}
            end
        end  
    end

    def destroy
        @outgoing_job.destroy
        respond_to do |format|
            format.html {redirect_to current_user}
            format.json { head :no_content}
        end
    end



    private
    def outgoing_job_params
        params.require(:outgoing_job).permit(:outgoing_job_value, 
            :sent,
            :confirmed,
            :done,
            :due_job_id,
            :user_id )
    end
 end

due_jobs 的 Controller 本质上是相同的。

但是,当我这样做时,我认为:

<% OutgoingJob.all.each do |od| %>
    <table class="table table-striped table-responsive">
        <thead>
            <tr>
                <th>ID</th>
                <th>Done By</th>
                <th>Done for</th>
                <th>Beneficiary</th>
                <th>Amount proposed</th>
                <th>Amount to paid</th>
                <th>Create due job</th>
                <th>Actions</th>
            </tr>
        </thead>

        <% if (od.confirmed == true) && (od.done== false) %>
            <tbody>
                <tr>
                    <td><%= od.id %></td>
                    <td><%= od.user.first_name %> <%= od.user.last_name %></td>
                    <td><%= od.due_job.user.first_name %> <%= od.due_job.user.last_name %></td>
                    <td><%= od.due_job.user.user_detail %></td>
                    <td>$ <%= number_with_delimiter(od.outgoing_job_value, delimiter: ',') %> </td>
                    <td> <%= --- %> </td>

                    <td>
                        <%= simple_form_for (DueJob.new) do |u| %>
                            <%= u.hidden_field :due_job_value, value: od.outgoing_job_value  %>
                            <%= u.hidden_field :user_id, value: od.user.id %>
                            <%= u.fields_for od  do |f| %>
                            <%= f.hidden_field :done, value: true %>
                        <%end%>
                        <%= u.submit "create", class: "btn btn-success" %>
                      <%end%>
                   </td>
                   <td><%= link_to "View", od %></td>
                </tr>
            </tbody>
        <%end%>
    </table>
    .....

使用嵌套表单,我可以为 DueJob 创建一条新记录,但它不会更新传出作业。我缺少什么?

最佳答案

我建议您使用ActiveRecord callbacks将一些代码归入您的模型(我怀疑尝试从一个 View 完成所有事情是可行的方法)。

DueJob 模型中添加如下内容:

class DueJob < ActiveRecord::Base
  # Register a callback to execute every time you create a new DueJob
  after_create :update_done_flag_on_outgoing_job

  # ... and add some code for that callback.
  def update_done_flag_on_outgoing_job
    outgoing_job.update_attribute :done, true
  end

仅通过阅读您的源代码,我就很难理解您如何识别新创建的 DueJob 与您想要更新的特定 OutgoingJob 记录之间的连接。就像 @coreyward 指出的那样,如果您能够更简洁地呈现您的代码,将会很有帮助。如果您无法按原样使用我的示例代码段,我想您始终可以根据您的需求调整 update_done_flag_on_outgoing_job 方法。

希望您觉得这有帮助。

关于ruby-on-rails - 如何在 Rails 中使用一种表单创建另一个模型时更新模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41475321/

相关文章:

javascript - 访问网页后尝试查找任何元素时 Watir 超时

Javascript 中的 HAML 中的 Javascript

javascript - PHP:我可以使用 AJAX 在表单中提交表单吗?

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

ruby - Rails 4 用索引重命名列

ruby-on-rails - 如何在 Rails 应用程序中实现 TCP 套接字?

ruby-on-rails - 在哪里存储 Rails 插件的配置

ruby-on-rails - Rails 3 数据建模帮助 - 具有许多、属于、嵌套属性

javascript - 渲染集合时避免使用多个 javascript_include_tag

ruby-on-rails - Multi-Tenancy Rails 应用程序中租户的自定义 View 和 Assets ?