Giter VIP home page Giter VIP logo

templates's Introduction

README

  1. routes.rb resources :tests, only: [:index, :new, :create]
Rails.application.routes.draw do
  resources :tests
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
  1. rails g migration CreateClassesTable
class CreateTests < ActiveRecord::Migration[5.1]
  def change
    create_table :tests do |t|
      t.string :name, null: false
      t.timestamps
    end
  end
end
  1. bundle exec rake db:migrate

  2. app/controllers/tests_controller.rb

class TestsController < ApplicationController

  def index
    @tests = Test.all
  end

  def new
    @test = Test.new
  end

  def create
    @test = Test.new(test_params)

    if @test.save
      # render :index, cannot do this because it does not refresh the page
      redirect_to tests_path
    else
      # render error message
    end
  end

  private

  def test_params
    params.require(:test).permit(:name)
  end
end
  1. app/models/class.rb
class Test < ApplicationRecord
  validates :name, presence: true, uniqueness: true
end
  1. app/views/tests/index.html.erb
<% if [email protected]? %>
  <% @tests.each do |test| %>
    <%= test.name %>
  <% end  %>
<% end %>

<%= link_to "new", new_test_path %>
  1. app/views/tests/new.html.erb
<%= form_for @test, action: "create" do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
  <%= submit_tag("submit") %>
<% end %>

<form accept-charset="UTF-8" action="/tests" method="post">
  <label for="test_name">Name</label>
  <!--  need auth token-->
  <input id="test_name" name="test[name]" type="text"/>
  <input type="submit" name="comit" value="submit" />
</form>

templates's People

Contributors

jacklynhma avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.