简体   繁体   中英

Do something after a form_for is submitted in rails

I have a form_for , and after the user submits it, I want to do an if/else statement. How can I accomplish this?

You seem very new to Rails, I would recommend that you try out the scaffold generator in Rails so you can get an idea for the different files needed:

rails g scaffold foo name:string

Take a look at the files generated, and especially at the FoosController and you will see where the code goes to handle the incoming form from a form_for method, and you will even see an example of an if/else conditional created for you.

Then get yourself over to the Rails guides and continue your education.

Since, your question includes a Ruby on Rails tag, so I assume you would like to do something on the backend when a user submits the form. If so is the case, it is pretty easy.

You have a controller, and inside that controller, you've got an action that receives the data when somebody submits the form. You can write your code there as what to do.

On other hand, if you would like to use form's submit callback on front-end, through JS, you can jQuery to get that callback, and here is how.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM