$ rails
$ rails new --helpLet's go ahead and create your app. Specify PSQL database, and skip the Spring Gem because it crashes some important things which we will go over later. Honestly this tutorial is based on: https://github.com/zackn9ne/WDI_NYC_Apr14_String/tree/master/w08/d02/Zack_Levine/railer-ajerx/single_crud
$ rails new myapp --database=postgresql --skip-springEdit your config/database.yml file as follows for most basic of operations, and watch your indenting its a YML file:
default: &default adapter: postgresql pool: 5 timeout: 5000 development: first_rails=""Bundle all the GEM's in the Gemfile and, then exec the command rake db:create:all.
$ bundle exec rake db:create:allA quick check to make sure everything runs.
$ rails sNow let's make our models. Planet----- name:string image:string* we'll go ahead and expect a URL here diameter:integer life:boolean
$ rails g model Planet name:string image:string life:boolean weight:integerCommit this initialisation by raking!
$ rake db:migrateNow fix your routes at
config/routes.rb
1. change the rails splashpage to this 2. run rake:routes to do some automagic routes
root "cars#index" resources :cars
Make your controller file based on the Model you made earlier
-The naming convention for this is "model_controller.rb" -A hint is to $ rake routes, copy down the aspects on the far right, and iterate through them as methods to use this as a checklist in your controller file!Try to run your server, your gonna get a no view error
-Switch to views/ and $mkdir /controller (from the name above in plural format) -touch an index.html.erb file in thereGo back to your specific Controller file and add the CRSF prevention stuff
private def car_attributes #whiteliste, but these come out as a variable called car_attributes that the sysop can use only params.require(:car).permit(:year, :make, :model, :color) end
Let's do some Rails Console Work to Seed some test values
Boot up the Rails Console and get to work.Here you want to make a Car.new with some whitelisted params. Then check the car you just made by calling it and one of its freshly made params.
Brush up on your ActiveRecord Skills for this one.
> car1 = Car.new(model: "some", make: "bitchin ride") > car1.model > car1.saveAlternatively
> car1 = Car.create(model: "some", make: "bitchin")
Go to your Views Folder
Let's remember to setup views for each model on an equal directory level as <code>app/views/layouts</code>, eg <code>app/views/cars</code>. Check $ rake routes. It will imply the Controller#Action as folows:- index.html.erb
- create.html.erb
- new.html.erb
- edit.html.erb
- show.html.erb
- update.htmlerb
- destroy.html.rb
Check your schema.rb in order to setup your Rails Form Helper file. Here you will get a list of the parameters you can send to the DB.
Now you are going to have all the views based on rake routes, and in addition please go to the db/schema.rb file in order to especially see the available fields in your new.html.erb (in this case) file.
Rails Form Helpers are just like include files. They must be named _something.html.erb (in this case because we are using erb). Here is some code for one.
is the partial %> <%# This is the partial %> <%# This is the partial %> <%# This is the partial %>In your new.html.erb call the Form HelperNew Car
<%= form_for(@car) do |f| %> #url was cars_path #starts the loop for the model name, again cars_path means Prefix of cars GET part and go there, this partial GIVES the @car instance to edit.html.erb, or where it happened to be called <%# params.require(:car).permit(:year, :make, :model, :color) %> <%# form helpers %> #starts creating the attributes allowed from private in controller <%= f.text_field :make, { placeholder: "car make" } %> <%= f.text_field :model, { placeholder: "model" } %> <%= f.text_field :year, { placeholder: "car year" } %> <%= f.text_field :color, { placeholder: "car color" } %> <%= f.text_field :image, { placeholder: "image url" } %> <%= f.button :submit %>
Create an object
<%= render partial: "form", locals: { car: @car } %> <%# you are telling the forthcoming partial that your are using the car object you created in the new conteroller %>
Let's Get Some Sample Data
db/seeds.rbSubwayLine.create(name: "L", color: "yellow") stations = HTTParty.get("https://data.cityofnewyork.us/api/views/he7q-3hwy/rows.json") stations["data"].each do |station| db_station = Station.create({ address: station[9] }) subway_line_names = station[10].split("-").select {|name| SubwayLine.find_by(name: name)} subway_line_names.each do |line_name| Stop.create({ station_id: db_station.id, subway_line_id: SubwayLine.find_by(name: line_name).id }) end end
$ rake g models todos completed:boolean
$ rake db:create
No comments:
Post a Comment