After My Sinatra C.R.U.D.
Creating a SINATRA CRUD MVC App was a tough experience. Working on Sinatra also helped me understand the entire framework behind rails.Upon completing the Sinatra app I understood exactly how each part worked. At first I did not know if I would be able to create this app, but after following a few guidelines instructed by my cohort lead Dakota Martinez I was able to succeed and create an app that was perfectly aligned with the project requirements. When beginning my project I did not know what to begin with. However once again Flat Iron has many forms of information to help a student achieve greatness. I started with my associations, and then created a map on how I would begin my project. After creating how each model would be associated with one another, I then began to create my migrations with rake db:create_migration NAME=create_user for my users and rake db:create_migration NAME=create_review for my reviews. After that I ran rake db: migrate which created my schema. I then proceeded to create my controllers. Once that was done I finished up with my html to make it look aesthetically pleasing, and WA LAH! An app was created.
User.create vs User.new+User.save
User.create does two specific actions. It creates an object(s), and it saves to the database, if the validations passes. These two methods are called .new and .save. It will return no matter if the object was saved successfully or not to the database. This can be a problem that can be solved by using .new and .save separately. The parameters(hash,array of hashes) used inside the create method describe the attributes of the objects created.
@user = User.create(params)
User.save does only one specific thing, it saves. It helps with controlling code and the flow by returning true or false if the object was saved to the database. In an existing model it updates, and in a new model it creates to the database. User.save is used with User.new for an effective flow control and more clarity of any problems that should arise in the return value.
User.new is a new object that can be instantiated empty or instantiated, pre-set with attributes but not yet saved.
Below are some examples of both methods.
In the code below I used User.create.
post ‘/users’ do
@user = User.create(params)
session[:user_id] = @user.id
redirect "/users/#{@user.id}"
end
in this code below i use the user.new
post '/users' do
@user = User.new( params[:email], password:params[:password])
if @user.save
session[:id] = @user.id
redirect "/"
else
erb :'users/new'
end
end
Beginning my journey with Flat Iron was nothing short of amazing. I started believing that it would be something that came easily to me. After being with Flat Iron Coding School for sometime, and I now understand that it is something more than just hard work. Becoming successful at Flat Iron takes determination, and commitment far beyond the every day pursuit. In completing my Sinatra Project I have learned about associations between different parts of an MVC framework, I have learned how to create a webpage from scratch, and all while working, caring for, and homeschooling my three year old son on my own. Joining Flat Iron was the correct decision for me, and anyone looking to join the field or get a better understand of the concepts and the industry. Although I've had many ups and downs as this is my first year of coding, I believe the curriculum is made to empower the student. I would love to help and give back when I graduate Flat Iron because I now understand and trust the process Flat Iron has paved for me and all of its students. It is a great institution for anybody trying to become a coder and I highly recommend it to anyone, it’s that good!