lafferjm
lafferjm

Reputation: 194

Form without action Ruby on Rails

I have been recently working with Ruby on Rails and have run into an issue that I can not quite figure out. I need to create a bunch of form mockups, that do not function. That is they should have the submit button, but it should not do anything upon being clicked. Normally using html I would do something along the lines of

<form action="#">
</form>

Trying to convert this to use Rails form helpers, I have done the following

<%= form_tag "#" do %>
    <%= label_tag :username, "Username: " %>
    <%= text_field_tag :username %>
    <br />
    <%= label_tag :password, "Password: " %>
    <%= password_field_tag :password %>
    <br />
    <%= submit_tag "Login" %>
<% end %>

This generates a form that is similar to what I want to achieve, however when clicking the submit button it tries to access /# via post which is not the desired result. Currently the only thing I can think of to achieve this is to set the disabled attribute of the button, but is there a better way?

Upvotes: 7

Views: 3364

Answers (3)

ragnika
ragnika

Reputation: 166

Try

<% form_tag "#", :onSubmit => "return false" do %>

Upvotes: 3

ılǝ
ılǝ

Reputation: 3528

Unfortunately this can't be achieved with form helpers. Defining a form_for or a form_tag requires an action for the form. You can set

:action => "#" 

But this will require including the action in routes -> having a controller with action for it -> rendering some page yet again.

You could manipulate the form after loading with javascript however (sust remember to set :remote to true - ). Or alternatively, if you insist on using the form helpers - replace the submit_tag with a button_tag:

 <%= button_tag "Login", :type => 'button'%>

Upvotes: 3

Vidya
Vidya

Reputation: 30320

Have you tried with button_tag instead of submit_tag? See here. Just make sure you don't use the default, or you will be right back where you started.

Upvotes: 2

Related Questions