Ruby on Rails does not save to bank

Asked

Viewed 629 times

1

Good night, you guys!

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

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

Can you please tell me possible reasons?

well, I have as a model:

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

and as a 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 the lack of attention!

You’ve been watching me, folks.

  • What is the content of the create method in your Todolistscontroler class? This is the method where the object is saved in the database.

  • I forgot to create it, I will edit rs. create is: def create&#xA; @todo_list = ToDoList.new(params[:todo_list])&#xA;&#xA; respond_to do |format|&#xA; if @todo_list.save&#xA; format.html { redirect_to @todo_list, notice: 'Todo list was successfully created.' }&#xA; format.json { render json: @todo_list, status: :created, location: @todo_list }&#xA; else&#xA; format.html { render action: "new" }&#xA; format.json { render json: @todo_list.errors, status: :unprocessable_entity }&#xA; end&#xA; end&#xA; end

1 answer

2

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

- Action - Description

  • #index - Shows all records
  • #show - Shows only one record
  • #new - Shows form for new item
  • #create - Create new item
  • #Edit - Shows form to edit item
  • #update - Updates new item
  • #delete - Shows alert about deleting an item
  • #Destroy - Delete the item

In this case we have seen his new which was probably implemented to create a new Todolist, however it does not persist this Todolist.

To make the persistence of Todolist in the Create action (probably you can have others) you will give in @todolist (keeping in mind that this is a Model Todolist object and that is with the form data) the following command:

@todolist.save

This should save the todolist in the bank you are using.

One easy way to test it is:

In the terminal type:

rails console

This will open the Rails console, type:

todo_list = ToDoList.new
todo_list.save

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.