【问题标题】:Button for updating attribute of nested resource用于更新嵌套资源属性的按钮
【发布时间】:2016-01-11 15:44:45
【问题描述】:

我正在尝试使用按钮更新嵌套资源的属性,但无法使其正常工作。请求是 Gigs 的嵌套资源。 用户可以查看针对他的特定演出的所有请求的列表,每个请求后面都有一个用于“雇用”和“拒绝”的按钮。

我来的最接近的是;

<%= form_for ([@gig, @gig.requests.build]) do |h| %>
  <%= h.hidden_field :status, value: "hired" %>
  <%= h.submit "Hire", class: "btn" %>
<% end %>

正如我拥有.build 一样,它以正确的状态复制“请求”,但保持原始状态不变。我尝试过使用.update.patch,但这些都不起作用,而且我找不到变量列表,任何进一步的尝试都将毫无用处。

我也尝试过link_to 方法,例如。

link_to("Hire", gig_requests_path(@request, :status => "hired"), :method => :put, :confirm => "Are you sure?")

没有路由错误。

我似乎与表单方法最接近,尽管我认为链接方法不起作用,因为我可能由于资源嵌套而搞砸了。任何澄清将不胜感激!

以防万一:

def update
    @request = Request.find(params[:id])
      if @request.update(request_params)
      redirect_to gig_path(@gig)
    else
      render 'edit'
  end
end

private

def request_params
  params.require(:request).permit(:message, :gig_id, :status)
end

路线:

resources :gigs do
      resources :requests
end

【问题讨论】:

标签: ruby-on-rails nested-attributes


【解决方案1】:

最后我是这样做的:

路线:

resources :requests
      post 'request/:id/hired' => 'requests#hired', as: :hired_request
      get 'hired'

控制器:

def hired
  @request = Request.find(params[:id])
      @request.update(status: "hired")
      @request.save
      redirect_to gig_requests_path
      flash[:notice] = "MARKED AS HIRED"
end

和视图:

<td>
<%= button_to "Hire user", { action: "hired", id: request.id },
                                method: :post, data: { confirm: "Are you sure?" } %>
</td>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多