从Rails中的成员变量通过表单减少/增加。

huangapple go评论102阅读模式
英文:

Decrease/Increase from member variable in rails via form

问题

I have two classes A and B.
B belongs to A, and A has many Bs with resources configured as:

  1. resources :as do
  2. resources :bs
  3. 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:

  1. def increase()
  2. @b = b.find(params[:id])
  3. @b.amount = @b.amount + params[:amount]
  4. @.update
  5. 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 Bs with resources configured as:

  1. resources :as do
  2. resources :bs
  3. 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:

  1. def increase()
  2. @b = b.find(params[:id])
  3. @b.amount = @b.amount + params[:amount]
  4. @.update
  5. end

I Just cannot wrap my head around how to do it.

答案1

得分: 1

你可以创建一个自定义路由。在Rails指南中有许多非资源路由供您参考。

一种方法是使用以下方式:

  • 使用 put,因为这是一个更新请求
  • 使用 on: :member 来捕获路由上的 params[:id] 参数
  1. # routes.rb
  2. resources :as do
  3. resources :bs do
  4. put 'increase_amount', on: :member
  5. put 'decrease_amount', on: :member
  6. end
  7. end
  1. # bs controller
  2. def increase_amount()
  3. @b = B.find(params[:id])
  4. amount = @b.amount + params[:amount]
  5. if @b.update(amount: amount)
  6. ...
  7. else
  8. ...
  9. end
  10. end
  11. def decrease_amount()
  12. @b = B.find(params[:id])
  13. amount = @b.amount - params[:amount]
  14. if @b.update(amount: amount)
  15. ...
  16. else
  17. ...
  18. end
  19. end
  1. # 尚未验证这是否是正确的路由名称,请使用 rake routes 进行确认
  2. <%= form_tag(increase_amount_a_bs_path(@a, @b), method: :put) do %>
  3. <%= label_tag(:amount, "Increase Amount:") %>
  4. <%= number_field_tag(:amount) %>
  5. <%= submit_tag("Update Amount") %>
  6. <% end %>
  7. <%= form_tag(decrease_amount_a_bs_path(@a, @b), method: :put) do %>
  8. <%= label_tag(:amount, "Decrease Amount:") %>
  9. <%= number_field_tag(:amount) %>
  10. <%= submit_tag("Update Amount") %>
  11. <% 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 bs params[:id] on the route
  1. # routes.rb
  2. resources :as do
  3. resources :bs do
  4. put &#39;increase_amount&#39;, on: :member
  5. put &#39;decrease_amount&#39;, on: :member
  6. end
  7. end
  1. # bs controller
  2. def increase_amount()
  3. @b = B.find(params[:id])
  4. amount = @b.amount + params[:amount]
  5. if @b.update(amount: amount)
  6. ...
  7. else
  8. ...
  9. end
  10. end
  11. def decrease_amount()
  12. @b = B.find(params[:id])
  13. amount = @b.amount - params[:amount]
  14. if @b.update(amount: amount)
  15. ...
  16. else
  17. ...
  18. end
  19. end
  1. # haven&#39;t verified this is the correct route name, confirm using rake routes
  2. &lt;%= form_tag(increase_amount_a_bs_path(@a, @b), method: :put) do %&gt;
  3. &lt;%= label_tag(:amount, &quot;Increase Amount:&quot;) %&gt;
  4. &lt;%= number_field_tag(:amount) %&gt;
  5. &lt;%= submit_tag(&quot;Update Amount&quot;) %&gt;
  6. &lt;% end %&gt;
  7. &lt;%= form_tag(decrease_amount_a_bs_path(@a, @b), method: :put) do %&gt;
  8. &lt;%= label_tag(:amount, &quot;Decrease Amount:&quot;) %&gt;
  9. &lt;%= number_field_tag(:amount) %&gt;
  10. &lt;%= submit_tag(&quot;Update Amount&quot;) %&gt;
  11. &lt;% end %&gt;

As for the popup, I'm not sure what frontend library in your tech stack but Bootstrap comes to mind as an option, modal

huangapple
  • 本文由 发表于 2023年5月7日 02:26:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定