Navigation

    EDU LEVEL DEVTH IDE FORUM
    USERNAME
    내 정보 수정 쿠폰 관리
  • 로그아웃
    • Search
    • Categories
    • Recent
    • Popular
    • Tags
    1. Home
    2. Daniel Lee
    • Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Best
    • Groups

    Daniel Lee

    @Daniel Lee

    2
    Level
    0
    Reputation
    2
    Posts
    4
    Profile views
    0
    Followers
    0
    Following
    Joined Last Online

    Daniel Lee Follow

    Posts made by Daniel Lee

    • RE: Redirection

      Ruby on Rails 입니다

      posted in 구름IDE 관련
      Daniel Lee
    • Redirection

      간단한 것인데 초보자가 개념없이 접근하다보니 막혔습니다..

      샘플로 하나 만든것인데,
      http://crud-vxade.run.goorm.io/board
      입력칸에 가장 최근에 입력된 값이 불러들여지도록 하고 싶은데 방법을 못 찾고 있네요...

      board_controller.rb

      class BoardController < ApplicationController
        def index
      	  @posts = Post.all
        end
      	
      	def create
      		@post = Post.includes(:comments).order("created_at desc").limit(1).offset(0)
      		title = params[:title]
      		content = params[:content]
      		updated_at = params[:updated_at]
      		lee = params[:lee]
      	
      		Post.create(title: title, content: content, updated_at: updated_at, lee: lee)
      		redirect_to '/board'
      	end 
      	
      	def edit
      		@post = Post.find(params[:id])
      	end
      		
      	def update
      		@post = Post.find(params[:id])
      		@post.title = params[:title]
      		@post.content = params[:content]
      		
      		@post.lee = params[:lee]
      		@post.save
      		redirect_to '/board'
      	end
      	
      	def delete
      		@post = Post.find(params[:id])
      		@post.destroy
      		redirect_to '/board'
      
      	end
      end
      
      

      routes.rb

      Rails.application.routes.draw do 
      	
      	get '/board', to: 'board#index' 
      	post '/create', to: 'board#create' 
      	get '/edit', to: 'board#edit' 
      	post '/update', to: 'board#update' 
      	get '/delete', to: 'board#delete' 
      	# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 
      end
      

      index.html.erb

      <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
      
      <form action="/create" method="post">
      	
         <input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden">
      		  
        	제목 : <input type="text"  name="title"><br>
        	내용 : <input type="text"  name="content"><br>
      	이프로 : <input type="integer" name="lee"  /><br />
      	
        <input type="submit" value="Submit">
      
      
      <% @posts.reverse_each do |post| %>
      <hr>
      <h3> ID : <%= post.id %></h3>
      <h3> DATE : <%= post.updated_at %></h3>
      <h3> 제목 : <%= post.title %></h3>
      <p> 내용 :<%= post.content %></p>
      <p> 이프로 : <%= post.lee %> / 24</p>
      <a href="/edit?id=<%= post.id %>">수정하기</a>
      <a href="/delete?id=<%= post.id %>">삭제하기</a>
      <hr>
      <% end %>
      

      20210207094539_create_posts.rb

      class CreatePosts < ActiveRecord::Migration[6.0]
        def change
          create_table :posts do |t|
      		t.string :title
      		t.text :content
      		t.integer :lee
      	
            t.timestamps
          end
        end
      end
      
      

      edit.html.erb

      <form action="/update" method="post">
      	<input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden" />
      	
      	<input name="id" value="<%= @post.id %>" type="hidden"/>
      	제목 : <input type="text" name="title" value="<%= @post.title %>" /><br />
      	내용 : <input type="text" name="content" value="<%= @post.content %>" /><br />
      	이프로 : <input type="integer" name="lee" value="<%= @post.lee %>" /><br />
      	
      	<input type="submit" value="Submit" />
      </form>
      

      application.rb

      require_relative 'boot'
      
      require 'rails/all'
      
      # Require the gems listed in Gemfile, including any gems
      # you've limited to :test, :development, or :production.
      Bundler.require(*Rails.groups)
      
      module GoormRailsApp
        class Application < Rails::Application
          # Initialize configuration defaults for originally generated Rails version.
          config.load_defaults 6.0
      	config.active_record.default_timezone = :local
          # Settings in config/environments/* take precedence over those specified here.
          # Application configuration can go into files in config/initializers
          # -- all .rb files in that directory are automatically loaded after loading
          # the framework and any gems in your application.
      	  
        end
      end
      
      
      posted in 구름IDE 관련
      Daniel Lee
    • 1 / 1
    goorm
    상호 : (주)구름    대표 : 류성태    개인정보보호책임자 : 김현화
    경기 성남시 분당구 판교로 242 PDC A동 902호
    사업자등록번호 : 124-87-39200
    통신판매업신고번호 : 제2019-성남분당B-0224호
    TEL : 031-600-8586    Email : contact@goorm.io
    • 구름EDU
    • 구름LEVEL
    • 구름DEVTH
    • 구름IDE
    • 구름FORUM
    ⓒ goorm Inc. All Rights Reserved.
    채용 이용약관 개인정보처리방침 도움말 powered by nodeBB