2015年9月3日 星期四

[RoR]在View呼叫Controller中的Method

在View中呼叫Controller中的Method,目前有實作到的有兩種

方法一:直接使用


  • 宣告為helper_method
def bulk_import
    puts "bulk_import is work!!!"
  end
  helper_method :bulk_import
  • 在View中直接使用
  <%=bulk_import%>
  或
  <%bulk_import%>

方法二:透過Link或Button呼叫

  • 在routes裡新增
#給button_to使用
post "/button_test" => "customers#button_test"
#給link_to使用 
get "/button_test" => "customers#button_test"
  • 在View裡新增
<%= button_to 'Button Test', button_test_path %>
<%= link_to 'Button Test', button_test_path %>

沒有留言: