respond_to do |format| if@todo.save() format.html {redirect_to @todo, notice:'User was successfully created.'} else format.html {render :new} end end end
defedit puts `[edit]` end
defupdate puts `[update]`
respond_to do |format| if@todo.update(todo_params) format.html { redirect_to @todo, notice:'User was successfully updated.' } else format.html { render :edit } end end end
defdestroy puts `[destroy]` @todo.destroy
respond_to do |format| format.html { redirect_to todos_url, notice:'User was successfully destroyed.' } end end
private defset_todo puts '[set_todo]' @todo = Todo.find(params[:id]) end
deftodo_params params.require(:todo).permit(:Title,:Detail) end end
Rails.application.routes.draw do get 'todos', to:'todos#index' post 'todos', to:'todos#create' get 'todos/new', to:'todos#new',as:'new_todo' get 'todos/:id/edit', to:'todos#edit',as:'edit_todo' get 'todos/:id', to:'todos#show',as:'todo' patch 'todos/:id', to:'todos#update' put 'todos/:id', to:'todos#update' delete 'todos/:id', to:'todos#destroy' end
<%= form_with(model: todo,local:true) do |form|%> <% puts "_form"%> <% if todo.errors.any? %> <% puts "[ERR]"%> <divid="error_explanation"> <h2> <%= pluralize(todo.errors.count, "error") %> prohibited this user from being saved: </h2>
<ul> <% todo.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %>
Rails.application.configure do #~省略~ #末尾に以下を追加 config.action_mailer.default_url_options = { host:'localhost', port:3000 } end
Devise gem 設定 2
\ToDoApp\config\routes.rb を編集。 root to: "todos#index"を追記。
\ToDoApp\config\routes.rb
1 2 3 4 5 6 7 8 9 10 11 12 13
Rails.application.routes.draw do
get 'todos', to:'todos#index' post 'todos', to:'todos#create' get 'todos/new', to:'todos#new',as:'new_todo' get 'todos/:id/edit', to:'todos#edit',as:'edit_todo' get 'todos/:id', to:'todos#show',as:'todo' patch 'todos/:id', to:'todos#update' put 'todos/:id', to:'todos#update' delete 'todos/:id', to:'todos#destroy'
classTodo < ApplicationRecord belongs_to :user,optional:true end
コントローラ、ビューの追加
user がログインしてからの、画面となる。 コントローラとビューを作成します。
1
>rails generate controller Users index
\ToDoApp\app\controllers\users_controller.rb を修正。
1 2 3 4 5 6 7 8
classUsersController < ApplicationController before_action :authenticate_user!, only: [ :show] defindex if current_user.id!=nil @user = User.find(current_user.id) end end end
\ToDoApp\app\views\users\index.html.erb を修正。
1
<h1>INDEX</h1>
ルーティング修正
\ToDoApp\config\routes.rb を修正。 root to: "todos#index"を追記。
\ToDoApp\config\routes.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Rails.application.routes.draw do devise_for :users
get 'users', to:'users#index'
get 'todos', to:'todos#index' post 'todos', to:'todos#create' get 'todos/new', to:'todos#new',as:'new_todo' get 'todos/:id/edit', to:'todos#edit',as:'edit_todo' get 'todos/:id', to:'todos#show',as:'todo' patch 'todos/:id', to:'todos#update' put 'todos/:id', to:'todos#update' delete 'todos/:id', to:'todos#destroy'
respond_to do |format| if@todo.save() format.html {redirect_to @todo, notice:'User was successfully created.'} else format.html {render :new} end end end
users\index.html.erb を修正
\ToDoApp\app\views\users\index.html.erb を修正。 @user が持っている todos プロパティを表示する。 内容は、\ToDoApp\app\views\todos\index.html.erb を真似する。
get 'userinfos/:id/edit', to:'userinfos#edit',as:'edit_userinfo' patch 'userinfos/:id',to:'userinfos#update' put 'userinfos/:id',to:'userinfos#update' get 'userinfos/new',to:'userinfos#new',as:'new_userinfo' post 'userinfos',to:'userinfos#create' get 'userinfos/:id', to:'userinfos#show',as:'userinfo'
devise_for :users
get 'users', to:'users#index'
#get 'todos', to: 'todos#index' post 'todos', to:'todos#create' get 'todos/new', to:'todos#new',as:'new_todo' get 'todos/:id/edit', to:'todos#edit',as:'edit_todo' get 'todos/:id', to:'todos#show',as:'todo' patch 'todos/:id', to:'todos#update' put 'todos/:id', to:'todos#update' delete 'todos/:id', to:'todos#destroy'
defupdate respond_to do |format| if@userinfo.update(userinfo_params) format.html {redirect_to root_path ,notice:'was successfully updated.' } else format.html {render :edit} end end end
defnew @userinfo = Userinfo.new() end
defcreate @userinfo = Userinfo.new(userinfo_params) @userinfo.User_id = current_user.id respond_to do|format| if@userinfo.save() format.html {redirect_to root_path,notice:'User was successfully created.'} else format.html {redirect_to :new} end end end
private defset_userinfo @userinfo = Userinfo.find(params[:id]) end defuserinfo_params params.require(:userinfo).permit(:name,:profile) end end
get 'userinfos/:id', to:'userinfoss#show',as:'userinfo' get 'userinfos/:id/edit', to:'userinfos#edit',as:'edit_userinfo' patch 'userinfos/:id',to:'userinfos#update' put 'userinfos/:id',to:'userinfos#update' get 'userinfos/new',to:'userinfos#new',as:'new_userinfo' post 'userinfos',to:'userinfos#create'
devise_for :users
get 'users', to:'users#index'
#get 'todos', to: 'todos#index' post 'todos', to:'todos#create' get 'todos/new', to:'todos#new',as:'new_todo' get 'todos/:id/edit', to:'todos#edit',as:'edit_todo' get 'todos/:id', to:'todos#show',as:'todo' patch 'todos/:id', to:'todos#update' put 'todos/:id', to:'todos#update' delete 'todos/:id', to:'todos#destroy'
root to:"users#index"
end
userinfoss#showへのルーティングを用意しないと、後から出てくるform_withメソッドで、 ActionView::Template::Error (undefined method `userinfo_path' for #<#<Class:~~~>といったエラーが出る。
1
get 'userinfos/:id', to:'userinfoss#show',as:'userinfo'
<%= form_with(model: userinfo ,local:true) do |form|%> <% if userinfo.errors.any? %> <divid="error_explanation"> <h2> <%= pluralize(userinfo.errors.count, "error") %> prohibited this user from being saved: </h2>
<ul> <% userinfo.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %>