소개 응용 프로그램 만들기
rails new profile
cd profile
rails s #localhost3000で起動の確認
로그인 기능 설치 rails g controller comments index
login_app/config/routes.rbRails.application.routes.draw do
root 'comments#index' # ここを追記します
get 'comments/index' # 自動で設定されたルーティング
devise_for :users
end
login_app/gemfilegem 'devise' #適当に挿入
bundle install
이제 divise를 이용하는 데 필요한 파일을 만듭시다.rails g devise:install
config/environments/development.rbconfig.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
#上の方にはりつけ
app/views/layouts/application.html.erb<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%=yield%>하기 전에 이것을 보충하세요.rails g devise user #ログイン機能用のモデルを作成
rake db:migrate #マイグレート
rails g devise:views #ログイン機能に必要なビューファイルをまとめて作成
views/comments/index.html.erb<h1>ログインアプリ</h1>
<% if user_signed_in? %> <!-- ユーザーがログインしているか調べる -->
<!-- ユーザーがログインしていた時の処理 -->
<h4> メールアドレス: <%= current_user.email %> </h4>
<%= link_to "ログアウト", destroy_user_session_path, method: :delete %> <!-- ログアウトをする -->
<% else %>
<!-- ユーザーがログインしていなかった時の処理 -->
<h2> 現在ログインしていません </h2>
<%= link_to "新規登録", new_user_registration_path, class: 'post' %> <!-- 新規登録画面に移行する -->
<% end %>
원래의 물건을 지우고 전부 붙이다
추가 테이블 작성
rails g scaffold profile user_id:integer name:string age:integer address:string
rails g scaffold article user_id:integer profile_id:integer tittle:string content:string
#articleとprofileMのVCの作成
부자 관계를 맺다
model/user.rbclass User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_one :profile
has_many :articles
end
model/profile.rbclass Profile < ApplicationRecord
belongs_to :user
has_many :articles
end
model/profile.rbclass Profile < ApplicationRecord
belongs_to :user
has_many :articles
end
model/article.rbclass Article < ApplicationRecord
belongs_to :user
belongs_to :profile
end
모델 생성하기rake db:migrate
로그인 후 new 소개 만들기
comments/index.html.erb <%= link_to 'New Profile', new_profile_path %>
<% else %>
<%else%>에 붙여넣기
브라우저에서 작업 확인
로그인한 사용자가 생성 또는 편집할 수 있도록 허용
profiles_controller.rbclass ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /profiles or /profiles.json
def index
@profiles = Profile.all
end
# GET /profiles/1 or /profiles/1.json
def show
end
# GET /profiles/new
def new
@profile = Profile.find_or_create_by(:user_id => current_user.id)
redirect_to edit_profile_url(@profile)
end
# GET /profiles/1/edit
def edit
if @profile.user_id != current_user.id
raise ActionController::RoutingError.new("Not authorized")
end
end
# POST /profiles or /profiles.json
def create
@profile = Profile.new(profile_params)
respond_to do |format|
if @profile.save
format.html { redirect_to @profile, notice: "Profile was successfully created." }
format.json { render :show, status: :created, location: @profile }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /profiles/1 or /profiles/1.json
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: "Profile was successfully updated." }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# DELETE /profiles/1 or /profiles/1.json
def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url, notice: "Profile was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_profile
@profile = Profile.find(params[:id])
end
# Only allow a list of trusted parameters through.
def profile_params
params.require(:profile).permit(:user_id, :name, :age, :address)
end
end
profiles/index.html.erb<%= link_to 'New Article', new_article_path %>
마지막 행에 추가
Artical의 사용자ID 현재 로그인한 사용자의 ID
articles_controller.rb def new
@article = Article.new(:user_id => current_user.id)
end
user_id를 형식으로 변경할 수 없음
articles/_form.erb <div class="field">
<%= form.hidden_field :user_id %>
</div>
hidden_필드로 변경, label 떼기
Reference
이 문제에 관하여(소개 응용 프로그램 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/masao-isap10/items/02b62d7742f9cebb132a
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
rails g controller comments index
Rails.application.routes.draw do
root 'comments#index' # ここを追記します
get 'comments/index' # 自動で設定されたルーティング
devise_for :users
end
gem 'devise' #適当に挿入
bundle install
rails g devise:install
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
#上の方にはりつけ
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
rails g devise user #ログイン機能用のモデルを作成
rake db:migrate #マイグレート
rails g devise:views #ログイン機能に必要なビューファイルをまとめて作成
<h1>ログインアプリ</h1>
<% if user_signed_in? %> <!-- ユーザーがログインしているか調べる -->
<!-- ユーザーがログインしていた時の処理 -->
<h4> メールアドレス: <%= current_user.email %> </h4>
<%= link_to "ログアウト", destroy_user_session_path, method: :delete %> <!-- ログアウトをする -->
<% else %>
<!-- ユーザーがログインしていなかった時の処理 -->
<h2> 現在ログインしていません </h2>
<%= link_to "新規登録", new_user_registration_path, class: 'post' %> <!-- 新規登録画面に移行する -->
<% end %>
rails g scaffold profile user_id:integer name:string age:integer address:string
rails g scaffold article user_id:integer profile_id:integer tittle:string content:string
#articleとprofileMのVCの作成
부자 관계를 맺다
model/user.rbclass User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_one :profile
has_many :articles
end
model/profile.rbclass Profile < ApplicationRecord
belongs_to :user
has_many :articles
end
model/profile.rbclass Profile < ApplicationRecord
belongs_to :user
has_many :articles
end
model/article.rbclass Article < ApplicationRecord
belongs_to :user
belongs_to :profile
end
모델 생성하기rake db:migrate
로그인 후 new 소개 만들기
comments/index.html.erb <%= link_to 'New Profile', new_profile_path %>
<% else %>
<%else%>에 붙여넣기
브라우저에서 작업 확인
로그인한 사용자가 생성 또는 편집할 수 있도록 허용
profiles_controller.rbclass ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /profiles or /profiles.json
def index
@profiles = Profile.all
end
# GET /profiles/1 or /profiles/1.json
def show
end
# GET /profiles/new
def new
@profile = Profile.find_or_create_by(:user_id => current_user.id)
redirect_to edit_profile_url(@profile)
end
# GET /profiles/1/edit
def edit
if @profile.user_id != current_user.id
raise ActionController::RoutingError.new("Not authorized")
end
end
# POST /profiles or /profiles.json
def create
@profile = Profile.new(profile_params)
respond_to do |format|
if @profile.save
format.html { redirect_to @profile, notice: "Profile was successfully created." }
format.json { render :show, status: :created, location: @profile }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /profiles/1 or /profiles/1.json
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: "Profile was successfully updated." }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# DELETE /profiles/1 or /profiles/1.json
def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url, notice: "Profile was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_profile
@profile = Profile.find(params[:id])
end
# Only allow a list of trusted parameters through.
def profile_params
params.require(:profile).permit(:user_id, :name, :age, :address)
end
end
profiles/index.html.erb<%= link_to 'New Article', new_article_path %>
마지막 행에 추가
Artical의 사용자ID 현재 로그인한 사용자의 ID
articles_controller.rb def new
@article = Article.new(:user_id => current_user.id)
end
user_id를 형식으로 변경할 수 없음
articles/_form.erb <div class="field">
<%= form.hidden_field :user_id %>
</div>
hidden_필드로 변경, label 떼기
Reference
이 문제에 관하여(소개 응용 프로그램 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/masao-isap10/items/02b62d7742f9cebb132a
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_one :profile
has_many :articles
end
class Profile < ApplicationRecord
belongs_to :user
has_many :articles
end
class Profile < ApplicationRecord
belongs_to :user
has_many :articles
end
class Article < ApplicationRecord
belongs_to :user
belongs_to :profile
end
rake db:migrate
comments/index.html.erb
<%= link_to 'New Profile', new_profile_path %>
<% else %>
<%else%>에 붙여넣기브라우저에서 작업 확인
로그인한 사용자가 생성 또는 편집할 수 있도록 허용
profiles_controller.rbclass ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /profiles or /profiles.json
def index
@profiles = Profile.all
end
# GET /profiles/1 or /profiles/1.json
def show
end
# GET /profiles/new
def new
@profile = Profile.find_or_create_by(:user_id => current_user.id)
redirect_to edit_profile_url(@profile)
end
# GET /profiles/1/edit
def edit
if @profile.user_id != current_user.id
raise ActionController::RoutingError.new("Not authorized")
end
end
# POST /profiles or /profiles.json
def create
@profile = Profile.new(profile_params)
respond_to do |format|
if @profile.save
format.html { redirect_to @profile, notice: "Profile was successfully created." }
format.json { render :show, status: :created, location: @profile }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /profiles/1 or /profiles/1.json
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: "Profile was successfully updated." }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# DELETE /profiles/1 or /profiles/1.json
def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url, notice: "Profile was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_profile
@profile = Profile.find(params[:id])
end
# Only allow a list of trusted parameters through.
def profile_params
params.require(:profile).permit(:user_id, :name, :age, :address)
end
end
profiles/index.html.erb<%= link_to 'New Article', new_article_path %>
마지막 행에 추가
Artical의 사용자ID 현재 로그인한 사용자의 ID
articles_controller.rb def new
@article = Article.new(:user_id => current_user.id)
end
user_id를 형식으로 변경할 수 없음
articles/_form.erb <div class="field">
<%= form.hidden_field :user_id %>
</div>
hidden_필드로 변경, label 떼기
Reference
이 문제에 관하여(소개 응용 프로그램 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/masao-isap10/items/02b62d7742f9cebb132a
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /profiles or /profiles.json
def index
@profiles = Profile.all
end
# GET /profiles/1 or /profiles/1.json
def show
end
# GET /profiles/new
def new
@profile = Profile.find_or_create_by(:user_id => current_user.id)
redirect_to edit_profile_url(@profile)
end
# GET /profiles/1/edit
def edit
if @profile.user_id != current_user.id
raise ActionController::RoutingError.new("Not authorized")
end
end
# POST /profiles or /profiles.json
def create
@profile = Profile.new(profile_params)
respond_to do |format|
if @profile.save
format.html { redirect_to @profile, notice: "Profile was successfully created." }
format.json { render :show, status: :created, location: @profile }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /profiles/1 or /profiles/1.json
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: "Profile was successfully updated." }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# DELETE /profiles/1 or /profiles/1.json
def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url, notice: "Profile was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_profile
@profile = Profile.find(params[:id])
end
# Only allow a list of trusted parameters through.
def profile_params
params.require(:profile).permit(:user_id, :name, :age, :address)
end
end
<%= link_to 'New Article', new_article_path %>
articles_controller.rb
def new
@article = Article.new(:user_id => current_user.id)
end
user_id를 형식으로 변경할 수 없음articles/_form.erb
<div class="field">
<%= form.hidden_field :user_id %>
</div>
hidden_필드로 변경, label 떼기
Reference
이 문제에 관하여(소개 응용 프로그램 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/masao-isap10/items/02b62d7742f9cebb132a텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)