You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使link_to绑定整个div而非仅文本触发点击事件?

Solution to Make Entire Div Trigger Rails Post Request

Got it, let's adjust your code so clicking anywhere on that div fires the post request instead of just the "Boeken naar afas" text. Here are two solid approaches using Rails conventions:

This is the most straightforward Rails way—we'll replace your div with a link_to block that includes all your existing classes, and wraps the inner content. This makes the entire area clickable:

= link_to update_bookkeeping_claims_claim_manager_path(@claim_manager), method: :post, class: 'col-sm-6 col-md-5 col-lg-3 item ibox-title import'
  .box.boxBatenmanager
    .boxInsite.boxTop
      %i.fa.fa-exclamation-circle
      <!-- Keep all your original inner content here, including the "Boeken naar afas" text -->

Note: If you need to keep the move() function

If that original onclick="move()" is still necessary, just add it to the link_to attributes:

= link_to update_bookkeeping_claims_claim_manager_path(@claim_manager), method: :post, class: 'col-sm-6 col-md-5 col-lg-3 item ibox-title import', onclick: 'move();'
  .box.boxBatenmanager
    .boxInsite.boxTop
      %i.fa.fa-exclamation-circle
      <!-- Original inner content -->

Option 2: Use Rails UJS with Data Attributes

If you prefer to keep the div element instead of using an anchor tag, you can use Rails UJS (Unobtrusive JavaScript) to handle the click event. This requires that rails-ujs is loaded on your page (it's included by default in most Rails setups):

.col-sm-6.col-md-5.col-lg-3.item.ibox-title.import{ 
  data: { 
    remote: true, 
    method: :post, 
    url: update_bookkeeping_claims_claim_manager_path(@claim_manager) 
  },
  onclick: 'move();' # Keep this if you still need the `move()` function
}
  .box.boxBatenmanager
    .boxInsite.boxTop
      %i.fa.fa-exclamation-circle
      <!-- Original inner content -->

This tells Rails UJS to listen for clicks on the div, then send a POST request to your specified path.

Either approach will make the entire div clickable and trigger the desired action. Pick whichever fits your styling and structure needs best!

内容的提问来源于stack exchange,提问作者akin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:22:58