How to insert image into SQLite with Sequel and Sinatra? - ruby

I'm trying to insert images to each one of my blog posts using SQLite with Sequel and Sinatra.
DB.create_table :posts do
primary_key :id
String :title
String :content
Datetime :created_at
Datetime :updated_at
foreign_key(:user_id, :users, :type=>String)
end
What changes should I make to my db for that and how can insert the image from the view and later display it?
Any help or idea is highly appreciated.

Personally, if it's for a blog then I'd upload the file somewhere - whether that be a filesharing site (your Dropbox or something), and then write an img link to it in the article's text. Why complicate things?
Edit: I'm not suggesting embedding HTML in your document, but (such as in Markdown) an indicator that is then further processed into HTML later, much as the rest of the document will.
I'd also rename Post to Article, since POST is an HTTP verb and used in Sinatra, or else you end up with stuff like this:
post "/post" do
post = Post.new title: "First Post"

Related

How can you make the id in the URL be random?

So I am looking for a way to create a randomized id that will link to a lobby that will be shown when the shown method is called in rails. As of right now, the url would look like this: http://localhost:3000/lobby/2. I'm looking to replace that 2 with a randomly generated id so you could send that link to friends if you want them to join your lobby. Any ideas?
You should share a bit more information as Gavin said. Knowing what you have already tried can help us give you more/better information on how to proceed.
I hope this sends you the right direction. For generating random IDs you can use SecureRandom:
http://ruby-doc.org/stdlib-1.9.3/libdoc/securerandom/rdoc/SecureRandom.html
I'd recommend you add another column to the lobbies table, and make it unique:
add_column :lobbies, :secure_token, :string, null: false
add_index :lobbies, :secure_token, unique: true
Now, in your Lobby model, whenever it is created, you can generate a unique token. For simplicity, you can do something along these lines:
class Lobby < ActiveRecord::Base
before_create :generate_unique_secure_token
# Rest of the model's code
private
def generate_unique_secure_token
begin
self.secure_token = SecureRandom.urlsafe_base64
end while self.class.exists?(secure_token: secure_token)
end
end
This will generate a secure_token every time a Lobby is created. Now in the controller you can use the secure_token instead of the id to find your Lobby. For example:
class LobbiesController < ApplicationController
def show
#lobby = Lobby.find_by(secure_token: params[:id])
end
end
I won't give you the whole answer since it's great to learn these things, but if you have any questions feel free to ask me!
This is where you should start: URLSafe Base 64
Here's a Railscasts episode that's soft of similar to what you want, see if you can expand from there! If you're new to Rails, be sure to check out Railscasts. All the pro episodes are available for free on youtube!

Rails - How to use the 'has_secure_password' technique for emails

Rails 3.1.0 - Ruby 1.8.6
I'm trying to create a signup page. I came across this 'has_secure_password' with basically does all the magic for you to enter your password twice and checks if they are correct.
I'd like to use this technique to check on email address as well. If it's not possible, what is the easiest way for me to get the user to key in their email twice and check if they match?
Please help. I'm a rails noob.
Thanks
Rails has a validates_confirmation_of validation built in. If your model has
class User < ActiveRecord::Base
attr_accessor :email_confirmation
validates_confirmation_of :email
end
Then you just need an email confirmation text field in your form and rails will check that they match.

Trouble passing value from view to controller in Rails 3.1.1

I have two model Product and Category, they have has_many, and belongs_to association respectively. Now, what I am trying to do is when I click on particular category I want all the products of that category to be listed. How do I do that
here is my view
<p><%=link_to #product.category.name, show_by_category_products_path(#product.category.id)%> <%= #product.name%> <%=#product.category.id%><p>
and method in controller
def show_by_category
#products = Product.where("category_id=?", :id)
end
Thanks! (I know its simple stuff, but sometimes you get blind and can't see a straightforward way so you have to sought help of others)
EDIT
okay maybe I figured out a way to go around this.. but I am not sure if it is done in right way
Now my view and model looks something like this
<p><%=link_to #product.category.name, show_by_category_product_path(id: #product.id)%> <%= #product.name%>
def show_by_category
#prod = Product.find(params[:id])
#products = Product.where('category_id=?', #prod.category_id)
end
Tell me if this is right way?
Your find should look more like Category.find(params[:id]).products. But try to follow RESTful routing principles, and nest your resources. Rails will do much more for you.
I would recommend you to read at least Getting Started guide, because you are doing it wrong.

Facebook posting help, adding to facebook whenever a user creates a post

I think this question is a matter of writing nice ruby code, let me see what you guys think. I've already setup all the auth/access token stuff with omniauth and and fbgraph, what I can't seem to work out is how to integrate it when a user creates a post.
My app revolves around users making posts (made up of 'title' and 'content'), I'd like the post to be automatically shared on facebook or twitter or both, depending on the particular authentications the users has setup. And not share anywhere if the user has signed up conventionally without facebook/twitter.
How would I integrate a dynamic way to share the title and content of a user's post whenever they post automatically? I was thinking of some type of after_save to the post model but I can't get it working right. Thank you for any help is it very much appreciated.Also it would great if it was a method that allowed for furture expansion if I wanted to share links and pictures later on.
This is the only post while searching that sheds some light about sharing to both but I'm still confused :(
Easy way of posting on Facebook page (not a profile but a fanpage)
In your Post model have:
after_commit :share_content
def share_content
user.share_content title, content
end
Then in User model have:
def share_content title, content
# some conditionals with whatever stuff you have to determine whether
# it's a twitter and/or facebook update...
if go_for_twitter
twitter.update title
end
if go_for_facebook
facebook.feed! :message => title
# ...etc.
end
end

embed vs link on large mongoDB sets (ruby)

Embedded vs link
I'm looking for the fastest way to search a Newsletter document for a connected Email. So far I have used MongoMapper with one document for Newsletter and another for Email. This is getting really slow with +100k Emails.
I was thinking maybe its faster to embed the emails in an array inside Newsletter
since I'm really only interested in the email ('someemail#email.com')
and not any logic around it.
1) Is it possible at all to embed as much as 100k-500k emails in one document?
2) Is Mongoid better/faster for this?
I'm adding the email if it is not already in the collection by asking
email = newsletter.emails.first(:email => 'someemail#email.com')
unless email
email = Email.new(:email => 'someemail#email.com', :newsletter_id => self.id)
email.save
end
And I think this is where it all starts to hurt.
Here is how they are connected
Class Newsletter
include MongoMapper::Document
many :emails
...
end
Class Email
include MongoMapper::Document
key :email, String
key :newsletter_id, ObjectId
belongs_to :newsletter
end
would love for any help on this :)
There is a maximum document size of 16mb currently for MongoDB, MongoMapper or Mongoid will make no difference to this.
see http://www.mongodb.org/display/DOCS/Documents
Embedded documents should be considerably quicker though, if you can fit all the emails within the limit could be a squeeze.
If storing the whole email is to much, why not just store either an array or just embedded the emails address withing the newsletter with a reference to the full email.
You can then get the speed advantage you want, and keep the emails accessible outside of the newsletter.

Resources