Rails form to database setup - ruby-on-rails

I'm fairly new to Rails, and I have a few beginner's questions. I want to create a beta signup page for my web app, something like this http://www.realestapp.com/ . How do I go about initializing such a rails project - what gems should I install and what generate commands should I use? I know I need a User model, with a string attribute to save an email address. I want to save the user email address from the form into a sqlite3 database.
Thanks!

I'm a big fan of the nifty gem (https://github.com/ryanb/nifty-generators). It greatly reduces headaches. Add it to your gemfile and run bundle install. Then you can just:
rails generate nifty:scaffold User email:string

Related

How to access heroku postgres Database with ruby on rails in shopify app

Hello I am working on a shopify embedded app with ruby on rails and I must save some config parameters given by the user or me.
I couldn't find any solution other than using heroku postgres database.
I looked everywhere for a code to insert or select data from the heroku database but I was unsuccessful in the Doc they literally gave one line of code in Ruby section.
https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-ruby
in the Gemfile add :
gem 'pg'
I have no idea how to execute any Query on the database with ruby in my controller.
EDIT :
I did all the configuration for the pg gem all I need to know is some example of code on how to use it
Use case example:
The app home have Input and Save button.
User 1 Enter data in input and Click save.
User 1 close the app.
User 1 returns to the app.
User 1 finds the data he wrote in the input.
Please help.
It would be more usual to save configuration info as heroku config vars, rather than in Postgres.
heroku config will list the current vars and heroku config:set VAR_NAME=var_value will set vars. This is not Ruby code - it is a part of heroku setup.
If you truly want to write Ruby on Rails code to modify the database, you need to learn ActiveRecord
Solved It with the help of reddit community.
All I had to do is adding this in my controller:
#mydb = PG.connect(ENV['DATABASE_URL'])
Query Example
#mydb.exec('insert into table values (\'test2\',\'test2\',\'2\');')

Rails will not let me destroy scaffolding from nifty generator

I was following one of the railscasts tutorials and decided to install nifty generators. Well, being a rails noob I didn't realize that the way parameters are handled changed. Now I can't undo any of my changes. So far I managed to roll back the database but every time I try to run
rails destroy nifty:scaffold mymodel
I get the error message
attr_accessible is extracted out of rails into a gem. Please use new recommended protection model for params(strong_parameters) or add protected_attributes to your Gemfile to use old one.
So I did. I added
gem 'protected_attributes'
and ran
bundle install
Then I tried to destroy it and it errored out again. I really hope nifty didn't just screw up my project. Can anyone help?
Um, this not a real solution, but a possible workaround: if the output of the rails generate nifty:scaffold mymodel command is still in your terminal buffer, you could manually delete the files it created.
And if the output isn't available, you could do rails generate nifty:scaffold mymodel2 in order to see what files nifty:scaffold created before manually deleting the corresponding files for mymodel.
Not elegant, but it might get you over the hump.

Migrate to Devise in Ruby on Rails

I have an existing model that uses bcrypt to encrypt the user password.
I wonder how I could migrate to use devise?
Do I still install devise the normal way and change the model attribute via database migration?
Add devise to your Gemfile, run bundle install, then rails generate devise ModelName (change ModelName to actual name of your model). This should add all needed things to your model as well as create a migration. I think devise will know, that your model already exists and it won't try to create new table, but it will modify existing one instead.
Then just run rake db:migrate, restart server and you should be good to go.
Just to be sure, use git or some other versioning system. It's been a while since I last tried this, so make sure you can easily go back, if something goes wrong.

Error regarding ActiveAdmin in Ruby on Rails when customizing the menu

Mac OS X 10.7.3 Lion,
Ruby 1.9.2,
Rails 3.2.2,
Sass 3.2.3
Following this tutorial:
http://activeadmin.info/documentation.html
Following this video tutorial
http://www.youtube.com/watch?v=tAxlrHcEg9U
I add the activeadmin gem, run bundle install, then run
rails generate active_admin:install
rails generate active_admin:resource POST
Only after creating the app/admin/posts.rb and trying to run either
db migrate
rails server
fails with the error
uninitialized constant Post NameError
with out that posts.rb file i am able to run the admin interface error free.
I tried moving the sass-rails gem out side of the :assets in my gem file and re-running bundle install as suggested in another question, but to no avail I still have the error
according to the getting started active admin tutorial "Post" is suppose to be a module name so i assume the code above is calling a class method (ActiveAdmin as the class, register as the method) and sending the module as a parameter and the block do end
Regardless the error is implying that RoR doesn't know what Post is. As if it does not exist. Being new to rails i do not know how to navigate well, meaning i do not even know where this ActiveAdmin source file is in order to dig through it for a method Post
Thank you for the consideration and your time, I appreciate it.
The linked tutorial assumes that you have already created a model named Post (and have run rake db:migrate to link it to the database). The purpose of the rails generate active_admin:resource Post command is to tell ActiveAdmin that you want it to consider the Post model in part of what it does.
Historically, you'll see models like Post and User in Rails a lot -- these are the commonly used examples of creating a blogging application (a user can create blog posts).
So, whatever models you have in your application can be registered with ActiveAdmin by replacing Post with the name of your model.
Another note: while generators like this tend to be forgiving, a Post is a model that is defined in post.rb and is linked to a SQL table called posts. Be careful with things like upper- and lower-case, and singular and plurals. In Rails they all fit together in a special way.

What's your solution for 'contact forms' in Ruby on Rails applications?

With a quick Google search, one can find literally hundreds of examples for contact forms using PHP and/or JavaScript, but there don't seem to be any "ready-made" contact forms for Ruby on Rails. Do they exist? What do you use for contact forms in your Ruby on Rails apps?
Personally, I use an ActiveRecord model for mine as I like to keep a store of messages being sent, and then use an ActionMailer after saving a record to send an email.
Alternatively there is a gem called MailForm which allows you to build a form model without a database table, which will work with other gems such as Formtastic.
As for ready-made contact forms, I don't know of any (though they may well exist) as it's not particularly time-consuming to build one from scratch.
I was running into the same issue with wanting an easy to use out of the box Contact Form, but wasn't able to find one. So I've written ContactUs a Rails Engine that you can easily drop into any Rails 3+ application. I tried to keep it dead simple and as easily configurable as possible. It does require the Formtastic gem since I wanted an easy way to hook into peoples existing form styles though.
To install the Engine add the contact_us gem to your Gemfile:
gem 'contact_us', '~> 0.1.3'
Run bundle and the install rake task:
$ bundle
$ bundle exec rake contact_us:install
Then just modify the generated initializer in /config/initializers/contact_us.rb to have the email you want the form submissions sent to.

Resources