Search code examples
ruby-on-railsformsscaffolding

How can I embed a form (scaffold) into a home page using ROR?


I have created a scaffold called TRADER and gave it the entries

fname:string lname:string twitteruser:string email:string

I want the form visible on the index page for the app user to submit their entry. Upon submission, I want a pop up box to appear saying " Thank you for your submission." I do not want to show the user what they just submitted, and the DB that holds all the submissions is accessed with authentication only available to me.

Currently the blank form is located in localhost:3000/traders/new
then after submission, the app displays the submitted information.

First, How can I embed the form into the index page ?


Solution

  • Embedding your form

    In your index.html.erb, add the following line:

    <%= render 'form' %>
    

    Assuming that the form partial is stored under app/view/trader with a name _form.html.erb.

    Redirect

    In your traders_controller.rb, edit the create action so that the user is not redirected to what the entry they submitted. I assume that there is a line like redirect_to @trader upon a successful save on the database. Instead, use something like redirect_to root_path, or wherever you want the users redirected to.

    Pop-up box

    In your traders.js.coffee under app/assets/javascripts, add some codes to display alert box on a successful submission of the form. Something like:

    $("#your_form").submit(function(event) {
      alert("Thank you for your submission.");
    });