Created
October 12, 2017 09:52
-
-
Save ruhenheim/697198e123ee230096e5d1534dfc2e95 to your computer and use it in GitHub Desktop.
お気に入り動作が複数投稿があっても正常動作するまでに修正したパッチ
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
diff --git app/controllers/goods_controller.rb app/controllers/goods_controller.rb | |
index 119862d..e4826ad 100644 | |
--- app/controllers/goods_controller.rb | |
+++ app/controllers/goods_controller.rb | |
@@ -1,15 +1,12 @@ | |
class GoodsController < ApplicationController | |
def create | |
- micropost = current_user.microposts.find_by(params[:content]) | |
- good = current_user.goods.find_or_create_by(micropost_id: micropost.id) | |
- good.save | |
- redirect_to root_url | |
+ current_user.goods.find_or_create_by(micropost_id: params[:micropost_id]) | |
+ redirect_to :back | |
end | |
- | |
+ | |
def destroy | |
- micropost = current_user.microposts.find_by(params[:content]) | |
- good = current_user.goods.find_by(micropost_id: micropost.id) | |
- good.destroy | |
- redirect_to root_url | |
+ good = current_user.goods.find_by(micropost_id: params[:micropost_id]) | |
+ good.destroy if good.present? | |
+ redirect_to :back | |
end | |
end | |
diff --git app/models/user.rb app/models/user.rb | |
index 42cfcf6..d836d4c 100644 | |
--- app/models/user.rb | |
+++ app/models/user.rb | |
@@ -7,13 +7,13 @@ class User < ActiveRecord::Base | |
uniqueness: { case_sensitive: false } | |
validates :local, length: {maximum: 50} , presence: true, on: :update | |
validates :profile, length: {maximum: 400}, presence: true, on: :update | |
- | |
+ | |
# has_secure_passwordによって、簡単に認証機能を追加することができる | |
has_secure_password | |
- | |
+ | |
# それぞれのユーザーは複数の投稿を持つことができる。 | |
has_many :microposts | |
- | |
+ | |
has_many :following_relationships, class_name: "Relationship", | |
foreign_key: "follower_id", | |
dependent: :destroy | |
@@ -23,8 +23,10 @@ class User < ActiveRecord::Base | |
foreign_key: "followed_id", | |
dependent: :destroy | |
has_many :follower_users, through: :follower_relationships, source: :follower | |
- | |
- | |
+ has_many :goods | |
+ has_many :good_microposts, through: :goods, source: :micropostq | |
+ | |
+ | |
# 他のユーザーをフォローする | |
def follow(other_user) | |
@@ -41,12 +43,11 @@ class User < ActiveRecord::Base | |
def following?(other_user) | |
following_users.include?(other_user) | |
end | |
- | |
- | |
+ | |
+ | |
def feed_items | |
Micropost.where(user_id: following_user_ids + [self.id]) | |
end | |
- | |
- has_many:goods | |
+ | |
end | |
diff --git app/views/goods/good.html.erb app/views/goods/good.html.erb | |
index c66cc7e..466effe 100644 | |
--- app/views/goods/good.html.erb | |
+++ app/views/goods/good.html.erb | |
@@ -1,22 +1,22 @@ | |
-<%= render 'show_gravatar_for'%> | |
- <div class="col-md-8"> | |
- <%= render 'follow_form' if logged_in? %> | |
- <% if @user.microposts.any? %> | |
- <h3>Microposts (<%= @user.microposts.count %>)</h3> | |
- <ol class="microposts"> | |
- <%= render @microposts %> | |
- </ol> | |
- | |
- <% end %> | |
- </div> | |
- <div> | |
- | |
- <% if current_user.goods.find_by(micropost_id: micropost.id) %> | |
- <%= link_to 'いいね取り消し', good_path(micropost), method: :delete %> | |
- <% else %> | |
- <%= link_to 'いいね', goods_path, method: :post %> | |
- <% end %> | |
- | |
- </div> | |
- | |
-</div> | |
\ No newline at end of file | |
+<!--<%= render 'show_gravatar_for'%>--> | |
+<!-- <div class="col-md-8">--> | |
+<!-- <%= render 'follow_form' if logged_in? %>--> | |
+<!-- <% if @user.microposts.any? %>--> | |
+<!-- <h3>Microposts (<%= @user.microposts.count %>)</h3>--> | |
+<!-- <ol class="microposts">--> | |
+<!-- <%= render @microposts %>--> | |
+<!-- </ol>--> | |
+ | |
+<!-- <% end %>--> | |
+<!-- </div>--> | |
+<!-- <div>--> | |
+ | |
+<!-- <% if current_user.good_microposts.include?(micropost) %>--> | |
+<!-- <%= link_to 'いいね取り消し', good_path(micropost), method: :delete %>--> | |
+<!-- <% else %>--> | |
+<!-- <%= link_to 'いいね', goods_path, method: :post %>--> | |
+<!-- <% end %>--> | |
+ | |
+<!-- </div>--> | |
+ | |
+<!--</div>--> | |
\ No newline at end of file | |
diff --git app/views/microposts/_micropost.html.erb app/views/microposts/_micropost.html.erb | |
index b54c49a..6fb8ef6 100644 | |
--- app/views/microposts/_micropost.html.erb | |
+++ app/views/microposts/_micropost.html.erb | |
@@ -5,22 +5,28 @@ | |
<span class="timestamp"> | |
Posted <%= time_ago_in_words(micropost.created_at) %> ago. | |
<% if current_user == micropost.user %> | |
- <%= link_to "delate", micropost, method: :delate, data: { confirm: "Your sure?" } %> | |
+ <%= link_to "delate", micropost, method: :delete, data: { confirm: "Your sure?" } %> | |
<% end %> | |
</span> | |
- | |
- | |
+ | |
+ | |
<div> | |
- | |
- <% if current_user.goods.find_by(micropost_id: micropost.id) %> | |
- <%= link_to 'いいね取り消し', good_path(micropost.id), method: :delete %> | |
+ | |
+ <% if current_user.good_microposts.include?(micropost) %> | |
+ <%= form_for(current_user.goods.find_by(micropost_id: micropost.id), html: {method: :delete, style: 'display: inline;'}) do |f| %> | |
+ <%= hidden_field_tag :micropost_id, micropost.id %> | |
+ <%= f.submit 'いいね取り消し' %> | |
+ <% end %> | |
<% else %> | |
- <%= link_to 'いいね', goods_path, method: :post %> | |
+ <%= form_for(current_user.goods.build, html: {style: 'display: inline;'}) do |f| %> | |
+ <%= hidden_field_tag :micropost_id, micropost.id %> | |
+ <%= f.submit 'いいね' %> | |
+ | |
+ <% end %> | |
<% end %> | |
- | |
+ | |
</div> | |
- | |
- | |
+ | |
+ | |
</li> | |
- | |
\ No newline at end of file | |
diff --git db/seeds.rb db/seeds.rb | |
index 4edb1e8..d2e643d 100644 | |
--- db/seeds.rb | |
+++ db/seeds.rb | |
@@ -5,3 +5,11 @@ | |
# | |
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) | |
# Mayor.create(name: 'Emanuel', city: cities.first) | |
+10.times do |c| | |
+ _name = "test" + c.to_s | |
+ user = User.create(name: _name, email: _name + "@test.jp", password: _name) | |
+ 10.times do |a| | |
+ _post = "test post " + a.to_s | |
+ user.microposts.create(content: _post) | |
+ end | |
+end | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment