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

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

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:

  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 Bs 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 bs params[:id] on the route
# routes.rb

resources :as do
  resources :bs do
    put &#39;increase_amount&#39;, on: :member
    put &#39;decrease_amount&#39;, 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&#39;t verified this is the correct route name, confirm using rake routes
&lt;%= form_tag(increase_amount_a_bs_path(@a, @b), method: :put) do %&gt;
  &lt;%= label_tag(:amount, &quot;Increase Amount:&quot;) %&gt;
  &lt;%= number_field_tag(:amount) %&gt;
  &lt;%= submit_tag(&quot;Update Amount&quot;) %&gt;
&lt;% end %&gt;

&lt;%= form_tag(decrease_amount_a_bs_path(@a, @b), method: :put) do %&gt;
  &lt;%= label_tag(:amount, &quot;Decrease Amount:&quot;) %&gt;
  &lt;%= number_field_tag(:amount) %&gt;
  &lt;%= submit_tag(&quot;Update Amount&quot;) %&gt;
&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:

确定