dimanche 11 janvier 2015

Cannot save simple form element string to DB (Rails, SqLite)

So I have a simple flow, where I am filling out a simple form, saving some basic information to a database, and then want to spit out that information on a page, specific to that item.


Everything is working as expected (form submission, redirect to the correct page (/books/:id), but once I am at that page, I cannot extract the string I saved and if I query the database using select * from books; I get the following results, showing that the record is successfully inserted, but without the name string, it seems.



4||2015-01-11 09:59:48.177701|2015-01-11 09:59:48.177701


So as you can see, the form is being submitted and the book record is succesfully created, but the "name" string is not being inserted.


Help much appreciated!!


My new book form (/app/views/books/new.html)



<h1>Create a new e-book</h1>

<%= form_for @book do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>

<p class="button"><%= f.submit %></p>
<% end %>


The books_controller.rb



class BooksController < ApplicationController

def index
@books = Book.all
end

def new
@book = Book.new
end

def show
@book = Book.find(params[:id])
end

def create
@book = Book.new(book_params)

@book.save
redirect_to @book
end

private

def book_params
params.require(:book).permit(:name)
end

end


The book.rb model



class Book < ActiveRecord::Base
end


The migration file where I created the table



class CreateBooks < ActiveRecord::Migration
def change
create_table :books do |t|
t.string :name

t.timestamps
end
end
end

Aucun commentaire:

Enregistrer un commentaire