7 Replies - 2206 Views - Last Post: 19 December 2012 - 12:12 PM Rate Topic: -----

#1 Spykedjadedragon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 23-November 08

Ruby/Sinatra POST to root of page

Posted 11 December 2012 - 07:17 PM

Hello, I'm working on this inventory web app written with Sinatra, and I'm having difficulty routing new inventory entries to the end of an item list which is displayed on the index of the page.

Here is my main.rb

require 'rubygems'
require 'sinatra'
require 'data_mapper'

DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/development.db")

class Item
  include DataMapper::Resource

  property :id, Serial
  property :name, String, :required => true
  property :quantity, Integer, :required => true
  property :notes, Text
end

get '/' do
  erb :list
# erb :new
end

# View an item
get '/item/:id' do
  @item = Item.get(params[:id])
  erb :item
end


DataMapper.auto_upgrade!

post '/' do
  # @item = params[:name]
  item = Item.new(:name => params[:name])
  params[:name]

  # if item.save
  #   status 201
  #   redirect '/'+icdtem.id.to_s
  # else
  #   status 412
  #   redirect '/'
  # end
end



Here is my list.erb file:

<table>
<% Item.each do |item| %>
<tr>
  <td><%= item.name  %></td>
  <td><%= item.quantity %></td>
  <td><%= item.notes %></td>
</tr>
<% end %>
</table>

<form action="/" method="POST">
  <input type="text" name="item" id="item">
  <input type="submit" value="Add Item" />
</form>




When I run this, it displays my current inventory list just fine, with a text field for me to enter new items at the bottom.
Whenever I try to enter in a new item however, it doesn't display on the '/' page. I've refreshed and I'm still looking at the same list I had when I started.

When I check the Sinatra web logs, it does show that I made a POST request...

This post has been edited by xclite: 11 December 2012 - 07:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Ruby/Sinatra POST to root of page

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 902
  • View blog
  • Posts: 3,163
  • Joined: 12-May 09

Re: Ruby/Sinatra POST to root of page

Posted 11 December 2012 - 07:19 PM

Is the item actually being saved to the database, however?
Was This Post Helpful? 0
  • +
  • -

#3 Spykedjadedragon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 23-November 08

Re: Ruby/Sinatra POST to root of page

Posted 11 December 2012 - 07:30 PM

That's a good question. I'm very new to this and new to databases especially. How can I test that the Items I input are actually being saved?
Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 902
  • View blog
  • Posts: 3,163
  • Joined: 12-May 09

Re: Ruby/Sinatra POST to root of page

Posted 11 December 2012 - 07:33 PM

You'll need to connect to whatever database is being used.

If you're in development and it's hitting the sqlite database, you need to do something like
sqlite3 <put_the_path_here>/development.db
It could also be sqlite instead of sqlite3.

And then run some commands to see what's in the tables. I suggest playing around with the direct interface to the database because it's great for troubleshooting issues like this.

I think .help is the sqlite command for getting help.
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,204
  • Joined: 21-June 11

Re: Ruby/Sinatra POST to root of page

Posted 12 December 2012 - 01:03 AM

You're never actually saving the item to the database anywhere. Your call to save is commented out.
Was This Post Helpful? 2
  • +
  • -

#6 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 902
  • View blog
  • Posts: 3,163
  • Joined: 12-May 09

Re: Ruby/Sinatra POST to root of page

Posted 12 December 2012 - 08:21 AM

Where's the fun if they don't find that themselves? =p
Was This Post Helpful? 0
  • +
  • -

#7 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 64
  • View blog
  • Posts: 735
  • Joined: 30-November 09

Re: Ruby/Sinatra POST to root of page

Posted 19 December 2012 - 11:27 AM

You can say:

x = Item.new(...)
x.save



Or to make it simpler

Item.create(...)


Which creates and saves a new record in the database in one method.

This post has been edited by heyoman1: 19 December 2012 - 11:27 AM

Was This Post Helpful? 1
  • +
  • -

#8 Spykedjadedragon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 23-November 08

Re: Ruby/Sinatra POST to root of page

Posted 19 December 2012 - 12:12 PM

Thanks for helping me out guys! I wasn't aware that I wasn't actually saving to the database. I'm going to try all these suggestions!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1