I would like to create relationships between three models: user, post and comment.
- User have many posts and comments
- Post have only one user and many comments
- Comment have one user and one post
so i create next migrations:
class Users < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps
end
end
end
class Posts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :content
t.integer :user_id
t.timestamps
end
end
end
class Comments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :title
t.string :content
t.integer :user_id
t.integer :post_id
t.timestamps
end
end
end
=============================================
models are next:
user.rb
class User < ActiveRecord::Base has_many :posts has_many :comments end
post.rb
class Post < ActiveRecord::Base belongs_to :user has_many :comments
endcomment.rb
class Comment < ActiveRecord::Base belongs_to :user belongs_to :post
end
===============================================
My users_controller.rb
class UsersController < ApplicationController
before_action :signed_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def index
@users = User.paginate(page: params[:page])
end
def show
@user = User.find(params[:id])
@posts = @user.posts.paginate(page: params[:page])
@comments = @user.comments.paginate(page: params[:page])
end
def new
@user = User.new(params[:user])
end
def edit
#@user = User.find(params[:id])
end
def update
#@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted."
redirect_to users_url
end
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# Before filters
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
now i want to create some actions for next tasks:
- For posts_controller.rb
1.1 create a post by user 1.2 delete a post by user 1.3 show user post with all comments 1.4 show all user posts
class PostsController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
@post = user.post.build(post_params)
@post = post.save
end
def destroy
@post.destroy
end
def show_user_post_with_all_comments
???
end
def show_all_user_posts
???
end
private
def post_params
params.require(:post).permit(:title, :content)
end
def correct_user
@post = current_user.posts.find_by(id: params[:id])
redirect_to root_url if @post.nil?
end
end
For comments_controller.rb 2.1 create a comment by user in post 2.2 delete a comment by user in post 2.3 show all user comments
2.4 find and show a post by user commentclass CommentsController < ApplicationController before_action :signed_in_user, only: [:create, :destroy] before_action :correct_user, only: :destroy
def create @comment = user.comment.build(comment_params) @comment = comment.save end def destroy @comment.destroy end def show_comment ??? end def show_all_user_comments ??? end def find_and_show_post_by_user_comment ??? end private def comment_params params.require(:comment).permit(:content) end def correct_user @comment = current_user.comments.find_by(id: params[:id]) redirect_to root_url if @comment.nil? end
end
Pls check for correct my migrations and models and help me with creating of actions with "???" in bodies Thank you much for your answers.