Rails 在编辑记录和创建时发送事件?

发布于 2024-10-22 11:26:17 字数 1520 浏览 4 评论 0原文

我的控制器中有以下代码:

  # GET /kases/1/edit
  def edit 
    @kase = Kase.find(params[:id])
    respond_to do |format|
      format.html { render :layout => 'kaseshow'} # show.html.erb
      format.xml  { render :xml => @kase }
      format.pdf { render :layout => false }
    end 
  end

  # POST /kases
  # POST /kases.xml
  def create
    @company = Company.find(params[:kase][:company_id])
    @kase = @company.kases.new(params[:kase])

    if @kase.save
        UserMailer.deliver_makeakase("[email protected]", "Highrise", @kase) if params[:sendtohighrise]
        UserMailer.deliver_makeakaseteam("[email protected]", "Highrise", @kase) if params[:notify_team_of_creation]
        @kase.create_freeagent_project(current_user) if params[:send_to_freeagent]

        redirect_to(@kase)

        #flash[:notice] = 'Case was successfully created.'
        flash[:notice] = fading_flash_message("Case was successfully created.", 5)
      else
        render :new
      end        
  end

我试图做到这一点,如果用户编辑案例,然后选择“发送到 Freeagent”复选框:

@kase.create_freeagent_project(current_user) if params[:send_to_freeagent]

则案例将重新发送到 Freeagent(在线会计软件)。我不担心处理重复项,因为如果 Freeagent 中已存在该案例,则用户无需重新发送它。

这可能吗?

I have the following code in my controller:

  # GET /kases/1/edit
  def edit 
    @kase = Kase.find(params[:id])
    respond_to do |format|
      format.html { render :layout => 'kaseshow'} # show.html.erb
      format.xml  { render :xml => @kase }
      format.pdf { render :layout => false }
    end 
  end

  # POST /kases
  # POST /kases.xml
  def create
    @company = Company.find(params[:kase][:company_id])
    @kase = @company.kases.new(params[:kase])

    if @kase.save
        UserMailer.deliver_makeakase("[email protected]", "Highrise", @kase) if params[:sendtohighrise]
        UserMailer.deliver_makeakaseteam("[email protected]", "Highrise", @kase) if params[:notify_team_of_creation]
        @kase.create_freeagent_project(current_user) if params[:send_to_freeagent]

        redirect_to(@kase)

        #flash[:notice] = 'Case was successfully created.'
        flash[:notice] = fading_flash_message("Case was successfully created.", 5)
      else
        render :new
      end        
  end

I am trying to make it so if the user edits a case and then selects the Send to Freeagent tickbox:

@kase.create_freeagent_project(current_user) if params[:send_to_freeagent]

then the case is resent to Freeagent (online accounting software). I'm not worried about dealing with duplicates because if the case already exists in Freeagent then the user won't need to resend it.

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素罗衫 2024-10-29 11:26:17

你的“更新”方法在哪里?仅当您加载数据并呈现用户将看到的用于编辑记录的页面时,才会调用编辑。当在该页面上单击保存/更新/提交时,它应该调用控制器中的“更新”方法。因此,您应该能够在 if 块的真实条件下将同一行放入“update”方法中,如下所示:

if @kase.update_attributes(params[:kase])
    #your code would go here
    @kase.create_freeagent_project(current_user) if params[:send_to_freeagent]
    #the rest of the code would go here
end

Where is your 'update' method? The edit is only called when you are loading the data and rendering the page the user will see to edit the record. When save/update/submit is clicked on that page, it should be calling the 'update' method in the controller. So, you should be able to put that same line into the 'update' method in the true condition of the if block that looks something like this:

if @kase.update_attributes(params[:kase])
    #your code would go here
    @kase.create_freeagent_project(current_user) if params[:send_to_freeagent]
    #the rest of the code would go here
end
挽袖吟 2024-10-29 11:26:17

是的,您可以在编辑表单中创建复选框/单选/隐藏字段。

如果你想为Kase创建一些模型,你也可以看看accepts_nested_attributes

It is, you could create checkbox/radio/hidden field in your edit form.

If you want to create some models for the Kase, you could as well look at accepts_nested_attributes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文