开发者

How to pass a lambda to link_to

开发者 https://www.devze.com 2023-04-13 08:10 出处:网络
I would like to use lambda as a parameter for link_to for the code below: edit.html.erb <h2>Edit customer info</h2>

I would like to use lambda as a parameter for link_to for the code below: edit.html.erb

<h2>Edit customer info</h2>

<%= render 'form' %>

<%= link_to(@return_to) do %>
  Back
  step_back()
<% end %>

Here is the def for step_back:

  #return link for prev开发者_C百科ious page in page step
  def step_back
    session[:page_step] -= 1
  end

The problem with the code above is that the step_back() is executed as soon as the edit.html.erb is loaded. Actually the step_back should only be executed when the user clicks the Back link. I figure that only lambda can accomplish this.

Any thoughts?


Your options are limited since you're interacting with the session.

You're getting that @return_to from somewhere; it'd probably be easiest to call an action that gets the same data and redirects to it, and does the same session manipulation.


See about writing your step_back() function in javascript and attaching it to an onClick html attribute on an tag instead of using the link_to rails helper

Then also .preventDefault() the event with javascript if you don't want the link to go anywhere, or to '#'

This will allow the code to execute on the click event in the browser and not during asset compilation before the page is served.

0

精彩评论

暂无评论...
验证码 换一张
取 消