Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 654
lafferjm Avatar asked Oct 10 '13 01:10

lafferjm


People also ask

What is form tag in Ruby on Rails?

The form_tag Rails helper generates a form withe the POST method by default, and it automatically renders the HTML that we were writing by hand before. Note: We can explicitly specify what HTTP verb to use for the form_tag if we want something other than POST .

What is local true in Rails form?

This setting determines whether form_with generates remote forms or not. It defaults to true. Or maybe more commonly set in config/application.


1 Answers

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'%>
like image 148
ılǝ Avatar answered Oct 25 '22 05:10

ılǝ