초등학생이라도 아는 Ruby on Rails 입문의 메모의 오사이에 북마크 등록 페이지 만들었다
추가 기능
드문 시작
rails g controller bookmarks index show new create destroy
rails g model bookmark title:string url:string
rake db:migrate
routes
config/routes.rb get 'bookmarks/index'
get 'bookmarks/show'
get 'bookmarks/new'
get 'bookmarks/destroy/:id' => "bookmarks#destroy"
post 'bookmarks' => "bookmarks#create"
새로운
app/views/bookmarks/new.html.erb<%= form_for Bookmark.new do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :url %>
<%= f.text_field :url %>
<%= f.submit %>
<% end %>
컨트롤러
app/controllers/bookmarks_controller.rbclass BookmarksController < ApplicationController
def index
@bookmarks = Bookmark.all
end
def show
end
def new
end
def create
@bookmark = Bookmark.new
@bookmark.title = params[:bookmark][:title]
@bookmark.url = params[:bookmark][:url]
@bookmark.save
redirect_to '/bookmarks/index'
end
def destroy
Bookmark.find(params[:id]).destroy
redirect_to '/bookmarks/index'
end
end
색인
app/views/bookmarks/index.html.erb<a href="/bookmarks/new">登録</a>
<ul>
<% @bookmarks.each do |bookmark| %>
<li><a href="<%=bookmark.url%>"><%=bookmark.title%></a>
| <a href="/bookmarks/destroy/<%=bookmark.id%>">削除</a>
<% end %>
</ul>
rails g controller bookmarks index show new create destroy
rails g model bookmark title:string url:string
rake db:migrate
get 'bookmarks/index'
get 'bookmarks/show'
get 'bookmarks/new'
get 'bookmarks/destroy/:id' => "bookmarks#destroy"
post 'bookmarks' => "bookmarks#create"
<%= form_for Bookmark.new do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :url %>
<%= f.text_field :url %>
<%= f.submit %>
<% end %>
class BookmarksController < ApplicationController
def index
@bookmarks = Bookmark.all
end
def show
end
def new
end
def create
@bookmark = Bookmark.new
@bookmark.title = params[:bookmark][:title]
@bookmark.url = params[:bookmark][:url]
@bookmark.save
redirect_to '/bookmarks/index'
end
def destroy
Bookmark.find(params[:id]).destroy
redirect_to '/bookmarks/index'
end
end
<a href="/bookmarks/new">登録</a>
<ul>
<% @bookmarks.each do |bookmark| %>
<li><a href="<%=bookmark.url%>"><%=bookmark.title%></a>
| <a href="/bookmarks/destroy/<%=bookmark.id%>">削除</a>
<% end %>
</ul>
보충 (초등학생도 알 수있는 Ruby on Rails 입문) | OpenBook 이번에 사용해 보자
Reference
이 문제에 관하여(초등학생이라도 아는 Ruby on Rails 입문의 메모의 오사이에 북마크 등록 페이지 만들었다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/tukiyo3/items/6615641ca273c64c159e텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)