英文:
Decrease/Increase from member variable in rails via form
问题
I have two classes A
and B
.
B
belongs to A
, and A
has many B
s with resources configured as:
resources :as do
resources :bs
end
B
has a member variable amount
which I want to be able to decrease and increase from the front end using a form, in this case, it cannot be the edit form.
Can I do this at all? and can I do where the form opens in a pop-up on following a link to?
The function I imagine for increase is something like this:
def increase()
@b = b.find(params[:id])
@b.amount = @b.amount + params[:amount]
@.update
end
I Just cannot wrap my head around how to do it.
英文:
I have two classes A
and B
B
belongs to A
and A
has many B
s with resources configured as:
resources :as do
resources :bs
end
B
has a member variable amount
which I want to be able to decrease and increase from the front end using a form, in this case, it cannot be the edit form.
Can I do this at all? and can I do where the form opens in a pop up on following a link to?
The function I imagine for increase is something like this:
def increase()
@b = b.find(params[:id])
@b.amount = @b.amount + params[:amount]
@.update
end
I Just cannot wrap my head around how to do it.
答案1
得分: 1
你可以创建一个自定义路由。在Rails指南中有许多非资源路由供您参考。
一种方法是使用以下方式:
- 使用
put
,因为这是一个更新请求 - 使用
on: :member
来捕获路由上的params[:id]
参数
# routes.rb
resources :as do
resources :bs do
put 'increase_amount', on: :member
put 'decrease_amount', on: :member
end
end
# bs controller
def increase_amount()
@b = B.find(params[:id])
amount = @b.amount + params[:amount]
if @b.update(amount: amount)
...
else
...
end
end
def decrease_amount()
@b = B.find(params[:id])
amount = @b.amount - params[:amount]
if @b.update(amount: amount)
...
else
...
end
end
# 尚未验证这是否是正确的路由名称,请使用 rake routes 进行确认
<%= form_tag(increase_amount_a_bs_path(@a, @b), method: :put) do %>
<%= label_tag(:amount, "Increase Amount:") %>
<%= number_field_tag(:amount) %>
<%= submit_tag("Update Amount") %>
<% end %>
<%= form_tag(decrease_amount_a_bs_path(@a, @b), method: :put) do %>
<%= label_tag(:amount, "Decrease Amount:") %>
<%= number_field_tag(:amount) %>
<%= submit_tag("Update Amount") %>
<% end %>
至于弹出窗口,我不确定您的技术堆栈中使用的前端库,但是Bootstrap是一个选项,modal。
英文:
You can create a custom route. Many diff non-resourceful routes in the Rails Guides to check out.
One way is with the following:
- using
put
because this is an update request - using
on: :member
to capture the bsparams[:id]
on the route
# routes.rb
resources :as do
resources :bs do
put 'increase_amount', on: :member
put 'decrease_amount', on: :member
end
end
# bs controller
def increase_amount()
@b = B.find(params[:id])
amount = @b.amount + params[:amount]
if @b.update(amount: amount)
...
else
...
end
end
def decrease_amount()
@b = B.find(params[:id])
amount = @b.amount - params[:amount]
if @b.update(amount: amount)
...
else
...
end
end
# haven't verified this is the correct route name, confirm using rake routes
<%= form_tag(increase_amount_a_bs_path(@a, @b), method: :put) do %>
<%= label_tag(:amount, "Increase Amount:") %>
<%= number_field_tag(:amount) %>
<%= submit_tag("Update Amount") %>
<% end %>
<%= form_tag(decrease_amount_a_bs_path(@a, @b), method: :put) do %>
<%= label_tag(:amount, "Decrease Amount:") %>
<%= number_field_tag(:amount) %>
<%= submit_tag("Update Amount") %>
<% end %>
As for the popup, I'm not sure what frontend library in your tech stack but Bootstrap comes to mind as an option, modal
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论