0

I have created the table 'kids' which will store names as strings and numbers as integers. Inserting the values manually using an SQL query in the terminal works fine.

I am new to Rails and cannot seem to find any simple examples that show how to grab data from an html form and insert it into my SQLite table.

Here is the form I am dealing with:

<form>
    Kid #1 Name:<input type="text"><br>
    Favorite Number:<input type="text"><br>

    Kid #2 Name:<input type="text"><br>
    Favorite Number:<input type="text"><br>

    <input type="submit" value="Submit">
</form>

What would the Controllers, Models, and Views look like in order to insert data from the form? It is 2 inserts into the table, but 1 form.

1 Answer 1

1

Create a method in your controller for making entries into the DB. Make a method, say add_kids_data in your KidsController. So your URL for form will be /kids/add_kids_data. Give names to your html text boxes appropriately. Say you name the boxes kid1, num1, kid2, num2, the values will be accessible in the controller as params[:kid1] and so on. You would want to create a method in you model that will actually create an entry in your DB. You can then simply call the method in your model from the controller with the params that you received from the form.

This RailsGuides post on form helpers will give you step-by-step detail of what you need to do to create a form.

Sign up to request clarification or add additional context in comments.

1 Comment

You could do it in lots of ways, but the whole idea of Rails is that, if you follow the Rails conventions for database interactions, it will save you tons of work. You could start reading this guide edgeguides.rubyonrails.org/…. In Paragraph 5.2 you will find a form and an explanation of what Rails will do for you. Also, reading on Rails and CRUD action could be helpful.

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.