用于更新嵌套资源属性的按钮
Button for updating attribute of nested resource
我正在尝试使用按钮更新嵌套资源的属性,但无法使其正常工作。 Requests 是 Gigs 的嵌套资源。
用户可以看到他的特定演出的所有请求列表,每个请求后面都有一个按钮 'hire' 和 'reject'。
我最接近的是;
<%= form_for ([@gig, @gig.requests.build]) do |h| %>
<%= h.hidden_field :status, value: "hired" %>
<%= h.submit "Hire", class: "btn" %>
<% end %>
因为我有 .build
它复制了具有正确状态的 'request' 但保持原始状态不变。我试过使用 .update
和 .patch
但它们不起作用,而且由于我找不到变量列表,任何进一步的尝试都是无用的。
我也试过link_to
方法,例如
link_to("Hire", gig_requests_path(@request, :status => "hired"), :method => :put, :confirm => "Are you sure?")
这给出了无路由错误。
我似乎与 form 方法最接近,尽管我认为 link 方法不起作用,因为我可能由于嵌套资源而搞砸了。任何澄清将不胜感激!
以防万一:
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
最后我是这样的:
路线:
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>
我正在尝试使用按钮更新嵌套资源的属性,但无法使其正常工作。 Requests 是 Gigs 的嵌套资源。 用户可以看到他的特定演出的所有请求列表,每个请求后面都有一个按钮 'hire' 和 'reject'。
我最接近的是;
<%= form_for ([@gig, @gig.requests.build]) do |h| %>
<%= h.hidden_field :status, value: "hired" %>
<%= h.submit "Hire", class: "btn" %>
<% end %>
因为我有 .build
它复制了具有正确状态的 'request' 但保持原始状态不变。我试过使用 .update
和 .patch
但它们不起作用,而且由于我找不到变量列表,任何进一步的尝试都是无用的。
我也试过link_to
方法,例如
link_to("Hire", gig_requests_path(@request, :status => "hired"), :method => :put, :confirm => "Are you sure?")
这给出了无路由错误。
我似乎与 form 方法最接近,尽管我认为 link 方法不起作用,因为我可能由于嵌套资源而搞砸了。任何澄清将不胜感激!
以防万一:
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
最后我是这样的:
路线:
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>