Ruby on Rails 입니다
Daniel Lee
@Daniel Lee
2
Level
0
Reputation
2
Posts
5
Profile views
0
Followers
0
Following
Posts made by 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