【问题标题】:Ruby on rails. Update different models from one link红宝石在铁轨上。从一个链接更新不同的模型
【发布时间】:2016-02-17 17:25:50
【问题描述】:

我需要同时更改两个不同模型的属性。我有一个gig 模型。演出has_manyrequests 我有一个表格可以将gig 的状态更改为filled,同时我想将演出请求的状态更改为“拒绝”。 (那些还没有“雇用”状态的人)

请求有status: string

Gig 有filled: boolean

请求模型:

scope :nothired, -> { where.not(status: 'hired') }

请求控制器:

def reject
  @request = Request.find(params[:id])
      @request.update(status: "reject")
      @request.save
      UserMailer.reject_notification(@request).deliver
      redirect_to gig_requests_path
      flash[:notice] = "MARKED AS REJECTED"
end

查看:

<%= simple_form_for @gig, :method => "put" do |t| %>
<% @gig.requests.nothired.each do |request| %>
<%= t.hidden_field :filled, :value => true %>

<%= t.submit t('dash.filltitle'), data: { confirm: "Are you sure?" }, class: "fill-gig-btn"  %>

<% end %>
<% end %>

现在我不确定如何格式化它以包含 requests 控制器操作。

我尝试通过隐藏字段传递 requests reject 操作(拒绝操作在控制器中并且它有效):

 <%= t.hidden_field { controller: "requests", action: "reject", id: request.id }, method: :post %>

但这给出了:

undefined method `{:url=>{:action=>"reject", :controller=>"requests", :id=>53}, :method=>:post}' for #<Gig:0x0055ef27d98cc8>

任何关于如何实现这一点的建议将不胜感激,谢谢。

【问题讨论】:

    标签: ruby-on-rails controller hidden-field update-attributes


    【解决方案1】:

    您可以在 Gig 控制器中执行此操作。

    因此,在 gig#update 操作中,您可以将所有子请求设置为“拒绝”,如下所示:

    @gig.requests.each do |request|
      request.update_attributes(status: 'reject')
    end
    

    您要确保只有在成功更新 @gig 时才会触发。

    因此,使用 callbacks 从 Gig 模型处理此问题可能更优雅

    【讨论】:

    • 我曾想过,但请求控制器中的“拒绝”操作设置为向每个创建请求的用户发送电子邮件。像这样更新状态不会触发该操作。
    • 我明白了。同样,也许您应该更努力地依靠回调来发送这些电子邮件并将所有代码从控制器中取出?您可以尝试将电子邮件放在 after_update 内的 Request 模型上?
    猜你喜欢
    • 2011-07-13
    • 2023-03-19
    • 1970-01-01
    • 2016-01-07
    • 2010-09-09
    • 1970-01-01
    • 2014-10-08
    • 2016-03-21
    • 1970-01-01
    相关资源
    最近更新 更多