Rails5でログインユーザーのみ下記の様にして表示しております。
index.html.erb で表示しております。
ruby
1 <% if user_signed_in? %> 2 <%= link_to fas_icon('plus') + ' 新しい記事を書く', new_article_path, class: 'btn btn-block btn-success' %> 3 <% end %>
ruby
1<% if user_signed_in? && article.user_id == current_user.id %> 2<% end %>
ですが、特定のユーザー例えば 1番目のユーザー(仮にid 1として)
このときどのように記述すれば ID1のユーザーだけ表示させることが出来ますか?
それとも、controllerの create あたりで何か記述をすればよいのでしょうか?
ご教授頂けますと幸いです。
articles_controller.rb は下記の様な感じです。
ruby
1 # POST /articles 2 # POST /articles.json 3 def create 4 @article = Article.new(article_params) 5 if user_signed_in? 6 @article.user_id = current_user.id 7 end 8 9 respond_to do |format| 10 if @article.save 11 format.html { redirect_to @article, notice: 'Article was successfully created.' } 12 format.json { render :show, status: :created, location: @article } 13 else 14 format.html { render :new } 15 format.json { render json: @article.errors, status: :unprocessable_entity } 16 end 17 end 18 end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/15 01:03