2

I'm struggling with the following problem: What's the correct HTML (and/or Rails) code for

  • a button that resides within a form, and
  • that should lead to a new page, but
  • should not submit anything (from the form)?

Basically, the button should work exactly like a hyperlink.

Thanks for your help!

Tom

3
  • It has to be a button simply for design reasons (consistency)... Commented Oct 19, 2010 at 16:08
  • Couldn't you just stylize it as a button with css? Commented Oct 19, 2010 at 16:25
  • @Trip: I can't style the buttons in this case, I have to use the raw browser format. And as far as I know, it's not possible to turn a hyperlink into a button, using CSS... Commented Oct 19, 2010 at 16:43

2 Answers 2

1

Old question, but for teh Googlez I have a non-JS solution. You can extract the button to a helper method, and then return the html for the button with the link generated by url_for. Use a dummy button as a link and you won't have to worry about doing something fancy to style the button.

def back_button
  target_url = url_for :controller => :users, :action => :new
  "<a href=#{target_url} style='text-decoration:none;'><button type='button'>&lt;&lt; Back</button></a>"
end
Sign up to request clarification or add additional context in comments.

Comments

0

You might try the "button_to" tag: Rails API

3 Comments

Unfortunately, this doesn't work: Usually, Rails would embed a submit button within an additional form. In my case, the button is already within a form, which apparently makes Rails drop the second additional form (probably because a form within a form is invalid HTML). Therefor, I'd end up with a submit button within a form, which POSTs the form - which should not happen...
Good point, I should've anticipated that. Well, there are a couple hacks. You could have a button image within a link (link_to image_tag ...), or you could use html/javascript for input type=button and have the onclick redirect you to a different page.
Thanks - the javascript proposal is the best, given my conditions. In Rails speak, the code then become: button_to_function 'Go to new page' do |page| page.redirect_to whatever_path end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.