質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

247閲覧

Ruby on rails でscaffoldのpathの設定がわかりません

ma_k

総合スコア33

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2019/08/08 07:39

###Ruby on Rails 5でブログ(NOTEと名づけてます)投稿のようなサイトを作っています。
###コメント投稿後、NOTEの投稿showに戻りたです。
scaffoldを使って、note投稿機能と、それにコメントできる投稿機能を追加しました。

コメントを投稿したあと,
投稿したコメントのNOTEに戻りたいのですが、
pathの設定がわからなくエラーが出ます。

誰かわかるかたご教授いただけたら嬉しいです。

html

1<div class="a-header"></div> 2<div class="mynote-main"> 3<p id="notice"><%= notice %></p> 4 5<p>コメント投稿しました</p> 6 7<p> 8 <strong>コメント</strong> 9 <%= @comment.body %> 10</p> 11 12 13 14<%= link_to '編集', edit_comment_path(@comment) %> | 15<%= link_to '戻る', note_path(@note.id:params[:note_id]) %> 16</div> 17

####<%= link_to '戻る', note_path(@note.id:params[:note_id]) %>を押して戻るのボタンを押して、NOTEの投稿showに戻りたいです。

path

1 comments GET /comments(.:format) comments#index 2 POST /comments(.:format) comments#create 3 new_comment GET /comments/new(.:format) comments#new 4 edit_comment GET /comments/:id/edit(.:format) comments#edit 5 comment GET /comments/:id(.:format) comments#show 6 PATCH /comments/:id(.:format) comments#update 7 PUT /comments/:id(.:format) comments#update 8 DELETE /comments/:id(.:format) comments#destroy 9 notes GET /notes(.:format) notes#index 10 POST /notes(.:format) notes#create 11 new_note GET /notes/new(.:format) notes#new 12 edit_note GET /notes/:id/edit(.:format) notes#edit 13 note GET /notes/:id(.:format) notes#show 14 PATCH /notes/:id(.:format) notes#update 15 PUT /notes/:id(.:format) notes#update 16 DELETE /notes/:id(.:format) notes#destroy 17

routes

1 2 resources :users, only: [:show ,:profile] 3 resources :comments,except:[:index] 4 resources :notes

html

1show.html.erb-notes 2 3<div class="a-header"></div> 4<div class="container"> 5 <div class="mynote-main"> 6 <p id="notice"> 7 <%= notice %> 8 </p> 9 <div class="note-wrapper-show"> 10 <h2> 11 <%= @note.title %> 12 </h2> 13 <p> 14 <%= render_with_hashtags(@note.content) %> 15 </p> 16 <h3 class="notename"> 17 <%= @note.user.name %> 18 </h3> 19 <p class="notetime"> 20 <%= @note.created_at.strftime('%Y/%m/%d') %> 21 </p> 22 <p> 23 <% if Good.find_by(user_id: current_user.id, note_id: @note.id) %> 24 <%= link_to("/goods/#{@note.id}/destroy", {method: "post"}) do%> 25 <i class="fas fa-check"></i> 26 <%= @notes_count %> いいね!済み 27 <% end %> 28 <% else %> 29 <%= link_to("/goods/#{@note.id}/create",{method: "post"}) do%> 30 <i class="far fa-thumbs-up"> </i> 31 <%= @notes_count %>いいね! 32 <% end %> 33 <% end %> 34 </p> 35 <p class="show-buttom"> 36 <% if @note.user_id == current_user.id %> 37 <%= link_to '編集', edit_note_path(@note),class: 'buttom'%> 38 <%= link_to '削除', @note, method: :delete, data: { confirm: 'Are you sure?' } ,class: 'buttom'%> 39 <% end %> 40 <%= link_to '戻る', notes_path ,class: 'buttom'%> 41 42 43 44 <div class="comment"> 45 <% @comments.each do |comment| %> 46 <h3 class="commentname"> 47 <%= comment.user.name %> 48 </h3> 49 <p> <%= comment.created_at.strftime('%Y/%m/%d') %></p> 50 <p class="commenttxt"> 51 <%= comment.body %> 52 </p> 53 <% if user_signed_in? && comment.user == current_user %> 54 <p> 55 <%= link_to '編集', edit_comment_path %> 56 <%= link_to comment_path(comment), method: :delete,data: { confirm: '削除してもよろしいですか?' } do %><i class="fas fa-trash-alt"></i> 57 </p> 58 <% end %> 59 <% end %> 60 61 <% end %> 62 <% if user_signed_in? %> 63 <%= render 'comments/form' %> 64 <% end %> 65 </div> 66 </div> 67 </div> 68 <footer> 69 <div class="footer-1"> <a href="/"> 70 <%= image_tag 'a-logo.gif',class:'a-logo' %></a> 71 </div> 72 </footer> 73 74

controller

1note.controller.erb 2 3class NotesController < ApplicationController 4 before_action :set_note, only: [:show, :edit, :update, :destroy] 5 before_action :ensure_correct_user,{only:[:edit,:update,:destroy]} 6 # GET /notes 7 # GET /notes.json 8 def index 9 @notes = Note.all.order(created_at: :desc) 10 11 12 end 13 14 # GET /notes/1 15 # GET /notes/1.json 16 def show 17 @notes_count = Good.where(note_id:@note.id).count 18 @note = Note.includes(:user).find(params[:id]) 19 @comments = @note.comments.all 20 @comment = @note.comments.build 21 22 end 23 24 25 def hashtag 26 @user = current_user 27 @tag = Hashtag.find_by(hashname: params[:name]) 28 @notes = @tag.notes.all.order(created_at: :desc) 29 30 end 31 32 33 34 35 # GET /notes/new 36 def new 37 @note = current_user.notes.new 38 end 39 40 # GET /notes/1/edit 41 def edit 42 end 43 44 # POST /notes 45 # POST /notes.json 46 def create 47 @note = Note.new(note_params) 48 49 respond_to do |format| 50 if @note.save 51 format.html { redirect_to @note, notice: 'Note was successfully created.' } 52 format.json { render :show, status: :created, location: @note } 53 else 54 format.html { render :new } 55 format.json { render json: @note.errors, status: :unprocessable_entity } 56 end 57 end 58 end 59 60 # PATCH/PUT /notes/1 61 # PATCH/PUT /notes/1.json 62 def update 63 respond_to do |format| 64 if @note.update(note_params) 65 format.html { redirect_to @note, notice: 'Note was successfully updated.' } 66 format.json { render :show, status: :ok, location: @note } 67 else 68 format.html { render :edit } 69 format.json { render json: @note.errors, status: :unprocessable_entity } 70 end 71 end 72 end 73 74 # DELETE /notes/1 75 # DELETE /notes/1.json 76 def destroy 77 @note.destroy 78 respond_to do |format| 79 format.html { redirect_to notes_url, notice: 'Note was successfully destroyed.' } 80 format.json { head :no_content } 81 end 82 end 83 84 85 def ensure_correct_user 86 @note = Note.find_by(id: params[:id]) 87 if @note.user_id!=current_user.id 88 flash[:notice]="権限がありません" 89 redirect_to("/notes") 90 end 91end 92 93 94 private 95 # Use callbacks to share common setup or constraints between actions. 96 def set_note 97 @note = Note.find_by(id: params[:id]) 98 end 99 100 101 # Never trust parameters from the scary internet, only allow the white list through. 102 def note_params 103 params.require(:note).permit(:title, :content, :user_id) 104 105 end 106end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

<%= link_to '戻る', "/notes/#{@comment.note_id}" %>
にしたらできました。

コメントしてくださった方お世話になりました。

投稿2019/08/10 01:34

ma_k

総合スコア33

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

note_path(@note)で大丈夫です。勝手に@noteからidを回収してくれます。

投稿2019/08/08 07:45

maisumakun

総合スコア145123

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ma_k

2019/08/08 07:54

回答ありがとうございます。 note_path(@note)に修正したら ActionController::UrlGenerationError in Comments#show No route matches {:action=>"show", :controller=>"notes", :id=>nil}, missing required keys: [:id] エラーが出ました。
winterboum

2019/08/08 14:22

このviewを呼ぶときに @note が設定されていないからです
ma_k

2019/08/09 01:45

ヒントありがとうございます。ちょっと修正してみます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問