Ruby on rails does not write to the database

1

Good evening, guys!

I created a small form with two fields and in the future I intend to expand this form with one or two fields. It happens that the insertion of the data in sqlite is not being done, but it does not give any error.

The application is being developed to be a to-do list.

Can you kindly tell me possible reasons?

Well, I have it as a model:

class ToDoList < ActiveRecord::Base attr_accessible :is_favorite, :name, :description has_many :tasks, dependent: :destroy belongs_to :member end

and as controller:

  class ToDoListsController < ApplicationController
    ...
     def new
        @todo_list = ToDoList.new

        respond_to do |format|
          format.html # new.html.erb
          format.json { render json: @todo_list }
        end
      end
    ...
     def create
    @todo_list = ToDoList.new(params[:todo_list])

    respond_to do |format|
      if @todo_list.save
        format.html { redirect_to @todo_list, notice: 'Todo list was successfully created.' }
        format.json { render json: @todo_list, status: :created, location: @todo_list }
      else
        format.html { render action: "new" }
        format.json { render json: @todo_list.errors, status: :unprocessable_entity }
      end
    end
  end
        end

Thank you in advance!

  

Edit

I noticed that I was calling the wrong params, calling it @todo_list and it was @to_do_list. It serves as an example of lack of attention!

Sail for attention, guys.

    
asked by anonymous 02.08.2014 / 01:21

1 answer

2

As spoken by Bernado Botelho usually when learning Rails a Rails controller has the following actions:

- Action - Description

  • #index - Show all records
  • #show - Shows only one record
  • #new - Show form for new item
  • #create - Creates the new item
  • #edit - Show form to edit item
  • #update - Update new item
  • #delete - Shows alert about deleting an item
  • #destroy - Delete item

In this case we have seen your new that was probably implemented to create a new TodoList, but it does not persist in this TodoList.

To do the persistence of the TodoList in the Create action (you can probably have others) you will give in the @todolist (keeping in mind that this is an TodoList object and that it has the form data) the following command :

@todolist.save

This should save todolist to the bank you are using.

One way to test this easy is:

At the terminal type:

rails console

This will open the Rails console, type:

todo_list = ToDoList.new
todo_list.save

If you do this, you should see a SQL query being displayed on the page (if you are using a SQL database), this means that it tried to save (and if you do not have a ROLLBACK it means that it saved)     

02.08.2014 / 02:16