Finish Gencode demo app

parent e2161372
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
// Place all the styles related to the Microposts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
class MicropostsController < ApplicationController
before_action :set_micropost, only: [:show, :edit, :update, :destroy]
# GET /microposts
# GET /microposts.json
def index
@microposts = Micropost.all
end
# GET /microposts/1
# GET /microposts/1.json
def show
end
# GET /microposts/new
def new
@micropost = Micropost.new
end
# GET /microposts/1/edit
def edit
end
# POST /microposts
# POST /microposts.json
def create
@micropost = Micropost.new(micropost_params)
respond_to do |format|
if @micropost.save
format.html { redirect_to @micropost, notice: 'Micropost was successfully created.' }
format.json { render action: 'show', status: :created, location: @micropost }
else
format.html { render action: 'new' }
format.json { render json: @micropost.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /microposts/1
# PATCH/PUT /microposts/1.json
def update
respond_to do |format|
if @micropost.update(micropost_params)
format.html { redirect_to @micropost, notice: 'Micropost was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @micropost.errors, status: :unprocessable_entity }
end
end
end
# DELETE /microposts/1
# DELETE /microposts/1.json
def destroy
@micropost.destroy
respond_to do |format|
format.html { redirect_to microposts_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_micropost
@micropost = Micropost.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def micropost_params
params.require(:micropost).permit(:content, :user_id)
end
end
module MicropostsHelper
end
class Micropost < ActiveRecord::Base
belongs_to :user
validates :content, length: { maximum: 140}
end
class User < ActiveRecord::Base
has_many :microposts
end
<%= form_for(@micropost) do |f| %>
<% if @micropost.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@micropost.errors.count, "error") %> prohibited this micropost from being saved:</h2>
<ul>
<% @micropost.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :content %><br>
<%= f.text_field :content %>
</div>
<div class="field">
<%= f.label :user_id %><br>
<%= f.number_field :user_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<h1>Editing micropost</h1>
<%= render 'form' %>
<%= link_to 'Show', @micropost %> |
<%= link_to 'Back', microposts_path %>
<h1>Listing microposts</h1>
<table>
<thead>
<tr>
<th>Content</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @microposts.each do |micropost| %>
<tr>
<td><%= micropost.content %></td>
<td><%= micropost.user_id %></td>
<td><%= link_to 'Show', micropost %></td>
<td><%= link_to 'Edit', edit_micropost_path(micropost) %></td>
<td><%= link_to 'Destroy', micropost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Micropost', new_micropost_path %>
json.array!(@microposts) do |micropost|
json.extract! micropost, :content, :user_id
json.url micropost_url(micropost, format: :json)
end
\ No newline at end of file
<h1>New micropost</h1>
<%= render 'form' %>
<%= link_to 'Back', microposts_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Content:</strong>
<%= @micropost.content %>
</p>
<p>
<strong>User:</strong>
<%= @micropost.user_id %>
</p>
<%= link_to 'Edit', edit_micropost_path(@micropost) %> |
<%= link_to 'Back', microposts_path %>
json.extract! @micropost, :content, :user_id, :created_at, :updated_at
SecondApp::Application.routes.draw do
resources :microposts
resources :users
# The priority is based upon order of creation: first created -> highest priority.
......
class CreateMicroposts < ActiveRecord::Migration
def change
create_table :microposts do |t|
t.string :content
t.integer :user_id
t.timestamps
end
end
end
......@@ -11,7 +11,14 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140718015025) do
ActiveRecord::Schema.define(version: 20140718024358) do
create_table "microposts", force: true do |t|
t.string "content"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "name"
......
require 'test_helper'
class MicropostsControllerTest < ActionController::TestCase
setup do
@micropost = microposts(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:microposts)
end
test "should get new" do
get :new
assert_response :success
end
test "should create micropost" do
assert_difference('Micropost.count') do
post :create, micropost: { content: @micropost.content, user_id: @micropost.user_id }
end
assert_redirected_to micropost_path(assigns(:micropost))
end
test "should show micropost" do
get :show, id: @micropost
assert_response :success
end
test "should get edit" do
get :edit, id: @micropost
assert_response :success
end
test "should update micropost" do
patch :update, id: @micropost, micropost: { content: @micropost.content, user_id: @micropost.user_id }
assert_redirected_to micropost_path(assigns(:micropost))
end
test "should destroy micropost" do
assert_difference('Micropost.count', -1) do
delete :destroy, id: @micropost
end
assert_redirected_to microposts_path
end
end
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
content: MyString
user_id: 1
two:
content: MyString
user_id: 1
require 'test_helper'
class MicropostsHelperTest < ActionView::TestCase
end
require 'test_helper'
class MicropostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment