質問するログイン新規登録

質問編集履歴

2

変更

2021/01/13 08:58

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,178 +1,3 @@
1
1
  NoMethodError in Posts#indexが解決できません、
2
2
 
3
- undefined method `username' for nil:NilClassが治りません。
3
+ undefined method `username' for nil:NilClassが治りません。
4
-
5
- ![イメージ説明](b72fe4bf981d7ea863c5698c2616f32a.png)
6
-
7
- やりたいこととしては、記事のリンクを「/ユーザー名/posts/記事のID」にしたいです。
8
-
9
-
10
-
11
- また、記事に直接URLでアクセスしたら、
12
-
13
- get ':username/posts/:id' => 'posts#show', as: :test_show
14
-
15
- 「http://localhost:3000/ユーザー名/posts/記事ID」
16
-
17
- でアクセスできるようにしたいのですが、「http://localhost:3000/ユーザー名/posts/記事ID」のユーザー名部分に適当な文字などを入力しても、同じ記事にアクセスできてしまう問題を解決したいです。
18
-
19
-
20
-
21
- # posts/index.html.erb
22
- ```ruby
23
- <h1>全ての投稿一覧</h1>
24
- <ul>
25
- <% @posts.each do |post| %>
26
- <li>
27
- <%= post.title %>(<%= post.set_created_date %>) | <%= link_to '詳細', test_show_path(username: @user.username, id: post.public_uid) %>
28
- </li>
29
- <% end %>
30
- </ul>
31
-
32
- ```
33
-
34
- # posts/show.html.erb
35
- ```ruby
36
- <h1><%= @post.title %></h1>
37
- <div>ユーザー名:@<%= link_to @user.username, "/#{@user.username}" %></div>
38
- <div>投稿日:<%= @post.set_created_date %></div>
39
- <div>更新日:<%= @post.set_updated_date %></div>
40
- <div>記事URL:<%= @post.public_uid %></div>
41
- <div><%= markdown(@post.content).html_safe %></div>
42
- <br>
43
- <%= link_to 'Twitterでシェア', "https://twitter.com/share?url=#{ request.url }&text=#{ @post.title }&hashtags=テスト", title: 'Twitter', target: '_blank' %>
44
- <%= link_to 'LINEでシェア', "https://social-plugins.line.me/lineit/share?url=#{ request.url }", title: 'LINE', target: '_blank' %>
45
- <%= link_to 'Facebookでシェア', "http://www.facebook.com/share.php?u=#{ request.url }", title: 'Facebook', target: '_blank' %>
46
- <br>
47
- <%= link_to '一覧に戻る', posts_path %>
48
-
49
- ```
50
-
51
- # routes.rb
52
- ```ruby
53
- Rails.application.routes.draw do
54
- devise_for :users, controllers: {
55
- :registrations => 'users/registrations',
56
- :sessions => 'users/sessions',
57
- :passwords => 'users/passwords'
58
- }
59
-
60
- root 'pages#index'
61
- resources :posts, except:[:show]
62
- get ':username/posts/:id' => 'posts#show', as: :test_show
63
- get ':username' => 'pages#show', as: :pages_show
64
- # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
65
- end
66
-
67
- ```
68
-
69
- #posts_controller.rb
70
- ```ruby
71
- class PostsController < ApplicationController
72
- # before_action :authenticate_user!
73
- before_action :sign_in_required, only: [:new, :create, :edit, :update, :destroy]
74
- before_action :baria_user, only: [:edit, :destroy, :update]
75
-
76
- def index
77
- @posts = Post.all.order(id: "DESC")
78
- @user = User.find_by(username: params[:username])
79
- end
80
-
81
- def show
82
- @post = Post.find_by(public_uid: params[:id])
83
- @user = User.find_by(id: @post.user_id)
84
- end
85
-
86
- def new
87
- @post = Post.new
88
- end
89
-
90
- def create
91
- @post = Post.new(post_params)
92
- if @post.save
93
- redirect_to post_path(@post.public_uid)
94
- else
95
- render 'new'
96
- end
97
- end
98
-
99
- def edit
100
- @post = Post.find_by(public_uid: params[:id])
101
- end
102
-
103
- def update
104
- @post = Post.find(params[:id])
105
- if @post.update(post_params)
106
- redirect_to post_path(@post.public_uid)
107
- else
108
- render 'edit'
109
- end
110
- end
111
-
112
- def destroy
113
- @post = Post.find_by(public_uid: params[:id])
114
- @post.destroy
115
- redirect_to posts_path
116
- end
117
-
118
- private
119
- # 仮に悪意のあるリクエスト(指定した以外のデータを送ってくる等)を受けた際に、.permitメソッドで許可していない項目については変更されず、データの扱いがより安全になります。
120
- def post_params
121
- params.require(:post).permit(:title, :content).merge(user_id: current_user.id)
122
- end
123
-
124
- def baria_user
125
- unless Post.find_by(public_uid: params[:id]).user_id == current_user.id
126
- flash[:notice] = "権限がありません"
127
- redirect_to posts_path
128
- end
129
- end
130
- end
131
-
132
- ```
133
-
134
- #user.rb
135
- ```ruby
136
- class User < ApplicationRecord
137
- has_many :posts, dependent: :destroy
138
-
139
- # Include default devise modules. Others available are:
140
- # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
141
- devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :confirmable, :lockable, :timeoutable, :trackable
142
-
143
- validates :username,
144
- uniqueness: true,
145
- length: { minimum: 3, maximum: 25 }
146
-
147
- def to_param
148
- return self.username
149
- end
150
- end
151
-
152
- ```
153
-
154
- #post.rb
155
- ```ruby
156
- class Post < ApplicationRecord
157
- belongs_to :users, optional: true
158
- generate_public_uid generator: PublicUid::Generators::HexStringSecureRandom.new(20)
159
- validates :title,
160
- presence: { message: 'は空白にできません'},
161
- length: { minimum: 5, maximum: 40 }
162
- validates :content,
163
- presence: true,
164
- length: { minimum: 120 }
165
- validates :user_id,
166
- presence: true
167
-
168
- def set_created_date
169
- created_at.strftime("%Y/%m/%d %H:%M")
170
- end
171
-
172
- def set_updated_date
173
- updated_at.strftime("%Y/%m/%d %H:%M")
174
- end
175
-
176
- end
177
-
178
- ```

1

更新

2021/01/13 08:58

投稿

退会済みユーザー
title CHANGED
@@ -1,1 +1,1 @@
1
- NoMethodError in Posts#indexが解決できない
1
+ nNoMethodError in Posts#indexが解決できない
body CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  undefined method `username' for nil:NilClassが治りません。
4
4
 
5
+ ![イメージ説明](b72fe4bf981d7ea863c5698c2616f32a.png)
6
+
5
7
  やりたいこととしては、記事のリンクを「/ユーザー名/posts/記事のID」にしたいです。
6
8
 
7
9