Skip to content

Commit

Permalink
User와 Notice, post 모델 관계 설정 1:N 및 edit, update기능 추가 및 예외처리
Browse files Browse the repository at this point in the history
  • Loading branch information
k352ex committed Nov 17, 2015
1 parent 70f0b65 commit 5df0569
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 37 deletions.
3 changes: 0 additions & 3 deletions app/assets/javascripts/cheditor/config.js

This file was deleted.

6 changes: 3 additions & 3 deletions app/assets/javascripts/ckeditor/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CKEDITOR.editorConfig = function( config ) {
config.enterMode = CKEDITOR.ENTER_BR;
}
// CKEDITOR.editorConfig = function( config ) {
// config.enterMode = CKEDITOR.ENTER_BR;
// }
8 changes: 1 addition & 7 deletions app/assets/stylesheets/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

#MainJumbotron {
border-radius: 6px;
padding-right: 60px;
padding-left: 60px;
}

.carousel-caption {
background: rgba(0, 0, 0, 0.5);
background: rgba(0, 0, 0, 0.8);
}
16 changes: 16 additions & 0 deletions app/controllers/freeboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def new

def create
@freeboard = Freeboard.new(freeboard_params)
@freeboard.user_id = params[:user_id]

if @freeboard.save
flash[:notice] = "글이 작성되었습니다."
Expand All @@ -24,6 +25,21 @@ def create
end
end

def edit
@freeboard = Freeboard.find(params[:id])
end

def update
@freeboard = Freeboard.find(params[:id])

if @freeboard.update(freeboard_params)
redirect_to @freeboard
else
render 'edit'
end
end


private

def freeboard_params
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/notice_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def new

def create
@post = Post.new(post_params)
@post.user_id = params[:user_id]

if @post.save
flash[:notice] = "공지가 작성되었습니다."
Expand All @@ -25,8 +26,18 @@ def show
@post = Post.find(params[:id])
end

def edit
@post = Post.find(params[:id])
end

def update
@post = Post.find(params[:id])

if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end

private
Expand Down
1 change: 1 addition & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Post < ActiveRecord::Base
belongs_to :user
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ class User < ActiveRecord::Base
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts
end
15 changes: 15 additions & 0 deletions app/views/freeboard/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div style="padding:10px"></div>

<div class="row">
<div class="col-xs-12 col-sm-8 col-sm-offeset-2 col-md-8 col-md-offset-2">
<%= form_for :freeboard, class: "form-control", url: freeboard_path(@freeboard), method: "patch" do |f| %>
<%= f.text_field :title , class: "form-control", :required => true, autofocus: true %>
<%= f.text_field :author, class: "form-control" %>
<!-- <%= f.text_area :content, size: "25x4" , class: "form-control" , placeholder: "내용" %> -->
<%= f.cktext_area :content, rows: 10 %>
<div class="pull-right" style="margin-top:3px">
<%= f.submit "제출", class: "btn btn-default" %>
</div>
<% end %>
</div>
</div>
20 changes: 13 additions & 7 deletions app/views/freeboard/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<%= form_for @freeboard, class: "form-control", url: {action: "create"} do |f| %>
<%= f.text_field :title , class: "form-control", placeholder: "타이틀", :required => true, autofocus: true %>
<%= f.text_field :author, class: "form-control", placeholder: "작성자" %>
<!-- <%= f.text_area :content, size: "25x4" , class: "form-control" , placeholder: "내용" %> -->
<%= f.cktext_area :content, rows: 10 %>
<%= f.submit "제출", class: "btn btn-default" %>
<% end %>
<div class="row">
<div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-8 col-md-offeset-2">
<%= form_for :freeboard, class: "form-control", url: {action: "create"} do |f| %>
<%= f.text_field :title , class: "form-control", placeholder: "타이틀", :required => true, autofocus: true %>
<%= f.text_field :author, class: "form-control", placeholder: "작성자" %>
<%= f.cktext_area :content, rows: 10 %>
<%= hidden_field_tag(:user_id, current_user.id) %>
<div class="pull-rigth">
<%= f.submit "제출", class: "btn btn-default" %>
</div>
<% end %>
</div>
</div>
39 changes: 29 additions & 10 deletions app/views/freeboard/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
<% if flash[:notice] %>
<div class="notice">
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<%= flash[:notice] %>
<div style="padding:20px"> </div>

<div class="row">
<% if flash[:notice] %>
<div class="notice">
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<%= flash[:notice] %>
</div>
</div>
</div>
<% end %>
<% end %>

<div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><%= raw @freeboard.title %></h3>
</div>
<div class="panel-body">
<%= raw @freeboard.content %>
</div>
</div>
<% if current_user.id == @freeboard.user_id %>
<div class="pull-right">
<%= link_to("Edit", edit_freeboard_path(@freeboard), class: "btn btn-default") %>
</div>
<% end %>
</div>
</div>

<div style="padding:30px"> </div>
<div style="padding:90px;"> </div>
6 changes: 3 additions & 3 deletions app/views/index/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>

<div class="carousel-inner" role="listbox">
<div class="carousel-inner" id="carousel-inner" role="listbox">
<div class="item active">
<%= image_tag("Coding.jpg", :width => "100%") %>
<div class="carousel-caption">
<h3> 안녕하세요, 원광대학교 컴퓨터공학과 학생 홈페이지입니다. </h3>
<h4> 원광대학교 컴퓨터공학과 홈페이지 </h4>
</div>
</div>
<div class="item">
<%= image_tag("Coding1.jpg", :width => "100%") %>
<div class="carousel-caption">
<h4> 학과 공지, 학년 별 공지, 학년 별 게시판 등이 있으니 이용해주시면 감사하겠습니다. </h4>
<h4> 학과 학년 별 공지, 게시판 등이 있으니 이용해주시면 감사하겠습니다. </h4>
</div>
</div>
<div class="item">
Expand Down
15 changes: 15 additions & 0 deletions app/views/notice/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div style="padding:10px"></div>

<div class="row">
<div class="col-xs-12 col-sm-8 col-sm-offeset-2 col-md-8 col-md-offset-2">
<%= form_for :post, class: "form-control", url: freeboard_path(@post), method: "patch" do |f| %>
<%= f.text_field :title , class: "form-control", :required => true, autofocus: true %>
<%= f.text_field :author, class: "form-control" %>
<!-- <%= f.text_area :content, size: "25x4" , class: "form-control" , placeholder: "내용" %> -->
<%= f.cktext_area :content, rows: 10 %>
<div class="pull-right" style="margin-top:3px">
<%= f.submit "제출", class: "btn btn-default" %>
</div>
<% end %>
</div>
</div>
1 change: 1 addition & 0 deletions app/views/notice/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<%= f.text_field :author, class: "form-control", placeholder: "작성자" %>
<!-- <%= f.text_area :content, size: "25x4" , class: "form-control" , placeholder: "내용" %> -->
<%= f.cktext_area :content, rows: 10 %>
<%= hidden_field_tag(:user_id, current_user.id) %>
<div class="pull-right">
<%= link_to("뒤로 가기", :back, class: "btn btn-default", role: "button") %>
<%= f.submit "제출", class: "btn btn-default" %>
Expand Down
7 changes: 5 additions & 2 deletions app/views/notice/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
<%= raw @post.content %>
</div>
</div>
<% if current_user.id == @post.user_id %>
<div class="pull-right">
<%= link_to("Edit", edit_notice_path(@post), class: "btn btn-default") %>
</div>
<% end %>
</div>
</div>

<div style="padding:90px;"> </div>
4 changes: 3 additions & 1 deletion db/migrate/20151113163651_create_posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ def change
t.string :title # 제목
t.string :content # 내용
t.string :author # 작성자


t.integer :user_id

t.timestamps null: false
end
end
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20151115111055_create_freeboards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def change
t.string :title
t.string :content
t.string :author
# t.string :body
t.integer :user_id

t.timestamps null: false
end
Expand Down
2 changes: 2 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
t.string "title"
t.string "content"
t.string "author"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Expand All @@ -41,6 +42,7 @@
t.string "title"
t.string "content"
t.string "author"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Expand Down

0 comments on commit 5df0569

Please sign in to comment.