-
Notifications
You must be signed in to change notification settings - Fork 0
Add forms #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Add forms #7
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6e75259
Rename columns postsCounter, likesCounter
KanoCode 8f7c2f9
set up comments and likes controller and actions
KanoCode d002a95
Add likes button for user
KanoCode 312c55c
fix linters
KanoCode 1e1623c
refactor to use comment_params
KanoCode bb029f9
refactor to use current_user
KanoCode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| class ApplicationController < ActionController::Base | ||
| def current_user | ||
| User.first | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| class CommentsController < ApplicationController | ||
| def new | ||
| @comment = Comment.new | ||
| end | ||
|
|
||
| def create | ||
| @comment = current_user.comments.new(comment_params) | ||
| @comment.post_id = params[:post_id] | ||
| if @comment.save | ||
| redirect_to user_post_path(user_id: current_user.id, id: @comment.post.id) | ||
| else | ||
| render :new, alert: 'Error occurred, Post not saved' | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def comment_params | ||
| params.require(:comment).permit(:text) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class LikesController < ApplicationController | ||
| def create | ||
| @post = Post.find(params[:post_id]) | ||
| @like = current_user.likes.new(like_params) | ||
| redirect_to user_post_path(user_id: current_user.id, id: @post.id) if @like.save | ||
| end | ||
|
|
||
| # def destroy | ||
| # @like = current_user.likes.find(params[:id]) | ||
| # @like.destroy | ||
| # redirect_to @post | ||
| # end | ||
|
|
||
| private | ||
|
|
||
| def like_params | ||
| params.require(:like).permit(:post_id, :user_id) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| module CommentsHelper | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| module LikesHelper | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| class Comment < ApplicationRecord | ||
| belongs_to :post | ||
| belongs_to :user | ||
| validates :text, presence: true | ||
| validates :text, length: { maximum: 100 } | ||
|
|
||
| after_save :updates_comments_counter | ||
| def updates_comments_counter | ||
| post.increment!(:commentsCounter) | ||
| post.increment!(:comments_counter) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <h1>Comments#create</h1> | ||
| <p>Find me in app/views/comments/create.html.erb</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <h1>Comments#index</h1> | ||
| <p>Find me in app/views/comments/index.html.erb</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <%= form_with method: :post,url:user_post_comments_path , model: @comment do |form| %> | ||
| <div> | ||
| <%= form.label :text %><br> | ||
| <%= form.text_area :text %> | ||
| </div> | ||
|
|
||
| <div> | ||
| <%= form.submit 'comment' %> | ||
| </div> | ||
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <h1>Likes#create</h1> | ||
| <p>Find me in app/views/likes/create.html.erb</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
|
|
||
|
|
||
| <%= form_with method: :post,url:user_posts_path , model: @post do |form| %> | ||
| <div> | ||
| <%= form.label :title %><br> | ||
| <%= form.text_field :title %> | ||
| </div> | ||
|
|
||
| <div> | ||
| <%= form.label :text %><br> | ||
| <%= form.text_area :text %> | ||
| </div> | ||
|
|
||
| <div> | ||
| <%= form.submit %> | ||
| </div> | ||
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <h2>new post </h2> | ||
|
|
||
| <%= render "form", post: @post %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,12 @@ | ||
| <h2><%= @post.title %> </h2> | ||
|
|
||
| <p><%= @post.text%> </p> | ||
| <%= link_to 'Comment', new_user_post_comment_path(user_id:@user.id,post_id:@post.id ) %> | ||
|
|
||
| <%= button_to "Like",user_post_likes_path(user_id:@user.id,post_id:@post.id ), params:{like:{post_id:@post.id } }, method: :post %> | ||
|
|
||
| <% @post.comments.each do |comment|%> | ||
| <li> <%= comment.text %> </li> | ||
|
|
||
| <%end%> | ||
| <%end%> | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| <li> | ||
| <div> | ||
| <img src="<%= user.photo%>" width=100 height=100/></div> <div><%= user.name%> <h5>Number of posts:</h5> <%= user.postsCounter%> </div> </li> | ||
| <img src="<%= user.photo%>" width=100 height=100/></div> <div><%= user.name%> <h5>Number of posts:</h5> <%= user.posts_counter%> </div> </li> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,4 @@ | ||
| <h2> These are all your users</h2> | ||
|
|
||
|
|
||
| <ul> | ||
| <% @users.each do |user| %> | ||
| <%= render user %> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| Rails.application.routes.draw do | ||
| # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html | ||
|
|
||
| # put '/post/:id/like' to: 'posts#like', as: 'like' | ||
|
|
||
| resources :users, only: [:index, :show] do | ||
| resources :posts, only: [:index, :show] | ||
| resources :posts, only:[:index, :show, :new, :create] do | ||
| resources :comments, only: [:new, :create] | ||
| resources :likes,only:[:create, :destroy] | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| class AddDefaultValueToPosts < ActiveRecord::Migration[7.0] | ||
| def change | ||
| remove_column :posts, :commentsCounter, :integer | ||
| add_column :posts, :comments_counter, :integer, default: 0 | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| class ChangeColumnInPosts < ActiveRecord::Migration[7.0] | ||
| def change | ||
| remove_column :posts, :likesCounter, :integer | ||
| add_column :posts, :likes_counter, :integer, default: 0 | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| class ChangeColumnNameInUsers < ActiveRecord::Migration[7.0] | ||
| def change | ||
| remove_column :users, :postsCounter, :integer | ||
| add_column :users, :posts_counter, :integer, default: 0 | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| require 'rails_helper' | ||
|
|
||
| # Specs in this file have access to a helper object that includes | ||
| # the CommentsHelper. For example: | ||
| # | ||
| # describe CommentsHelper do | ||
| # describe "string concat" do | ||
| # it "concats two strings with spaces" do | ||
| # expect(helper.concat_strings("this","that")).to eq("this that") | ||
| # end | ||
| # end | ||
| # end | ||
| RSpec.describe CommentsHelper, type: :helper do | ||
| pending "add some examples to (or delete) #{__FILE__}" | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| require 'rails_helper' | ||
|
|
||
| # Specs in this file have access to a helper object that includes | ||
| # the LikesHelper. For example: | ||
| # | ||
| # describe LikesHelper do | ||
| # describe "string concat" do | ||
| # it "concats two strings with spaces" do | ||
| # expect(helper.concat_strings("this","that")).to eq("this that") | ||
| # end | ||
| # end | ||
| # end | ||
| RSpec.describe LikesHelper, type: :helper do | ||
| pending "add some examples to (or delete) #{__FILE__}" | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'Comments', type: :request do | ||
| describe 'GET /index' do | ||
| it 'returns http success' do | ||
| get '/comments/index' | ||
| expect(response).to have_http_status(:success) | ||
| end | ||
| end | ||
|
|
||
| describe 'GET /new' do | ||
| it 'returns http success' do | ||
| get '/comments/new' | ||
| expect(response).to have_http_status(:success) | ||
| end | ||
| end | ||
|
|
||
| describe 'GET /create' do | ||
| it 'returns http success' do | ||
| get '/comments/create' | ||
| expect(response).to have_http_status(:success) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'Likes', type: :request do | ||
| describe 'GET /create' do | ||
| it 'returns http success' do | ||
| get '/likes/create' | ||
| expect(response).to have_http_status(:success) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'comments/create.html.erb', type: :view do | ||
| pending "add some examples to (or delete) #{__FILE__}" | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'comments/index.html.erb', type: :view do | ||
| pending "add some examples to (or delete) #{__FILE__}" | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'comments/new.html.erb', type: :view do | ||
| pending "add some examples to (or delete) #{__FILE__}" | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'likes/create.html.erb', type: :view do | ||
| pending "add some examples to (or delete) #{__FILE__}" | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
current_userhere 👏🏻There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @ShoiraTa
I will refactor my code