Won't save the attributes of a record

75 Views Asked by At

I'm trying to get the upgrade the example shown in this blog post

https://medium.com/@mitch_23203/the-exact-same-app-in-hyperstack-7f281cef46ca

to use hyperstack models like the followup blog post, but it just doesn't work.

Here is my code after I changed it:

class TodoIndex < HyperComponent

  def create_new_todo_item
    Todo.create(title: @todo)
    @title = nil
  end

  render(DIV, class: 'ToDo') do
    IMG(class: 'Logo', src: 'assets/logo.png', alt: 'Hyperstack Logo')
    H1(class: 'ToDo-Header') { 'Hyperstack To Do' }
    DIV(class: 'ToDo-Container') do
      DIV(class: 'ToDo-Content') do
        Todo.each do |item|
          TodoItem(key: item, item: item.title)
          .on(:delete_item) { item.destroy }
        end
      end
      DIV do
        INPUT(type: :text, value: @title)
        .on(:change) { |e| mutate @title = e.target.value }
        .on(:enter) { create_new_todo_item }
        BUTTON(class: 'ToDo-Add') { '+' }
        .on(:click) { create_new_todo_item }
      end
    end
  end
end

Everything seems to work fine, and new Todo's are saved but the attributes are nil. Like the title doesn't get saved.

1

There are 1 best solutions below

1
On

It should be:

def create_new_todo_item
  Todo.create(title: @title) # not @todo
  @title = nil
end

You set the title of Todo to its state value (@todo)...