So in my last post we set up a simple rails application with one route to your main index page.
Seeds file
To continue, we should add somethings to our db/seeds.rb
file.
Within your seeds.rb
file add the following hash:
items = {socks: 5, shoes: 2, dress: 3, shirts: 7, pants: 9}
Then iterate through the hash and create Items to your database, like this:
items.each{|item, quant| Item.create(name: item, quantity: quant)}
Now run rails db:seed
to update your database with your seeds.
Now, when you visit your localhost/3000/items in your browser you should see all the items you created in your seeds file.
Links
Next step was to add some links to a views page.
ActionView has a link_to method that allows you to add
Within your index page you can add the following links:
<%= link_to "New Item", new_item_path %>
The code above creates an href tag. The "New Item" will be between the a tag and the "new_item_path" will be the href.
Now you have to define your route for the new item within you config/routes.rb
:
get '/items/new', to: 'items#new', as: 'new_item'
That is it for today, tomorrow I will review the form_with rails helper.
Song of the day:
Top comments (0)