Skip to content
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

Creating likes #2

Merged
merged 5 commits into from
Nov 25, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Like Button Working
Beckyrose200 committed Nov 25, 2021

Verified

This commit was signed with the committer’s verified signature.
Murderlon Merlijn Vos
commit c34d8a98793d8efb80aa5f6b888d5ce8739e5c56
11 changes: 8 additions & 3 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -12,9 +12,9 @@ def index
@posts = Post.all
end

def update(id)
post = Post.find_by(id: id)
post.update(likes: (post.likes + 1 ))
def update
p post = Post.find_by(id: id_params)
post.update(likes: (post.likes.to_i + 1 ))
redirect_to posts_url
end

@@ -24,4 +24,9 @@ def update(id)
def post_params
params.require(:post).permit(:message)
end

def id_params
p params.require(:id)
end

end
8 changes: 4 additions & 4 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -60,13 +60,13 @@
<p class="card-text"><%= post.message %></p>




<%= button_to (post.likes.to_s + ' Likes'), post_path(post.id), :method => :patch %>


<button_to 'Add Like', update_path(post.id) id = '<%= post.message %>'><%= post.likes %> Likes </button>
<%= button_to 'Press me', update_path(post.id), method: :get %>
<a href="#" class="card-link">Comment</a>


<a href="#" class="card-link">Comment</a>
</div>
</div>
<div class="row">
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

resources :posts
end
5 changes: 5 additions & 0 deletions db/migrate/20211125153348_changing_likes_to_int.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChangingLikesToInt < ActiveRecord::Migration[6.0]
def change
change_column :posts, :likes, :integer, using: 'likes::integer'
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_11_23_160744) do
ActiveRecord::Schema.define(version: 2021_11_25_153348) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -19,7 +19,7 @@
t.string "message"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "likes"
t.integer "likes"
end

end