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

質問編集履歴

2

controllerとshowのviewsです

2015/08/09 12:00

投稿

shinogi1217
shinogi1217

スコア61

title CHANGED
File without changes
body CHANGED
File without changes

1

views

2015/08/09 12:00

投稿

shinogi1217
shinogi1217

スコア61

title CHANGED
File without changes
body CHANGED
@@ -15,4 +15,119 @@
15
15
  更新しようとしても
16
16
  ActiveRecord::RecordNotFound in NotesController#show
17
17
  Couldn't find Content with 'id'=
18
- のエラーメッセージがでてしまいます。
18
+ のエラーメッセージがでてしまいます。
19
+ ```ruby
20
+ <%- model_class = Note -%>
21
+ <div class="page-header">
22
+ <h1><%=t '.title', :default => model_class.model_name.human.titleize %></h1>
23
+ </div>
24
+
25
+ <dl class="dl-horizontal">
26
+ <dt><strong><%= model_class.human_attribute_name(:title) %>:</strong></dt>
27
+ <dd><%= @note.title %></dd>
28
+ <dt><strong><%= model_class.human_attribute_name(:note) %>:</strong></dt>
29
+ <dd><%= @note.content.name %></dd>
30
+ <dt><strong><%= model_class.human_attribute_name(:text) %>:</strong></dt>
31
+ <dd><%= @note.content.text %></dd>
32
+ </dl>
33
+
34
+ <%= link_to t('.back', :default => t("helpers.links.back")),
35
+ notes_path, :class => 'btn btn-default' %>
36
+ <%= link_to t('.edit', :default => t("helpers.links.edit")),
37
+ edit_note_path(@note), :class => 'btn btn-default' %>
38
+ <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
39
+ note_path(@note),
40
+ :method => 'delete',
41
+ :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
42
+ :class => 'btn btn-danger' %>
43
+ ```
44
+ この様に書きました。noteのcontrollerには
45
+ ```ruby
46
+ class NotesController < ApplicationController
47
+ before_action :set_note, only: [:show, :edit, :update, :destroy]
48
+
49
+ # GET /notes
50
+ # GET /notes.json
51
+ def index
52
+ @notes = Note.all
53
+ end
54
+
55
+ def mypage
56
+ @notes = Note.all
57
+ end
58
+
59
+ # GET /notes/1
60
+ # GET /notes/1.json
61
+ def show
62
+ @content = Content.find_by(note_id: params[:note_id])
63
+ @note = Note.find(params[:id])
64
+ end
65
+
66
+ # GET /notes/new
67
+ def new
68
+ @note = Note.new
69
+ @note.contents.build
70
+
71
+ end
72
+
73
+ # GET /notes/1/edit
74
+ def edit
75
+ @note = Note.find(params[:id])
76
+ end
77
+
78
+ # POST /notes
79
+ # POST /notes.json
80
+ def create
81
+ @note = Note.new(note_params)
82
+
83
+ respond_to do |format|
84
+ if @note.save
85
+ format.html { redirect_to @note, notice: 'Note was successfully created.' }
86
+ format.json { render :show, status: :created, location: @note }
87
+ else
88
+ format.html { render :new }
89
+ format.json { render json: @note.errors, status: :unprocessable_entity }
90
+ end
91
+ end
92
+ end
93
+
94
+ # PATCH/PUT /notes/1
95
+ # PATCH/PUT /notes/1.json
96
+ def update
97
+ @note = Note.find(params[:id])
98
+ respond_to do |format|
99
+ if @article.update(article_params)
100
+ format.html { redirect_to @article, notice: 'Article was successfully updated.' }
101
+ format.json { render :show, status: :ok, location: @article }
102
+ else
103
+ format.html { render :edit }
104
+ format.json { render json: @article.errors, status: :unprocessable_entity }
105
+ end
106
+ end
107
+ end
108
+
109
+ # DELETE /notes/1
110
+ # DELETE /notes/1.json
111
+ def destroy
112
+ @note = Note.find(params[:id])
113
+ respond_to do |format|
114
+ format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' }
115
+ format.json { head :no_content }
116
+ end
117
+ end
118
+
119
+ private
120
+ # Use callbacks to share common setup or constraints between actions.
121
+ def set_note
122
+ @note = Note.find(params[:id])
123
+ end
124
+
125
+ # Never trust parameters from the scary internet, only allow the white list through.
126
+ def note_params
127
+ params.require(:note).permit(
128
+ :title
129
+ contents_attributes: [:id, :name, :text, :note_id]
130
+ )
131
+ end
132
+ end
133
+ ```