scaffoldで作ったnoteにcontentというモデルの中身(カラム)を表示したい場合は
まず、noteのmodelとcontentのモデルにそれぞれ
ruby
1class Note < ActiveRecord::Base 2 has_many :contents, dependent: :destroy 3 accepts_nested_attributes_for :contents, allow_destroy: true 4end 5 6class Content < ActiveRecord::Base 7 belongs_to :note 8end 9
設定をすることまではできたのですが、controllerやshowのviwesにはどのように設定すればいいですか?
更新しようとしても
ActiveRecord::RecordNotFound in NotesController#show
Couldn't find Content with 'id'=
のエラーメッセージがでてしまいます。
ruby
1<%- model_class = Note -%> 2<div class="page-header"> 3 <h1><%=t '.title', :default => model_class.model_name.human.titleize %></h1> 4</div> 5 6<dl class="dl-horizontal"> 7 <dt><strong><%= model_class.human_attribute_name(:title) %>:</strong></dt> 8 <dd><%= @note.title %></dd> 9 <dt><strong><%= model_class.human_attribute_name(:note) %>:</strong></dt> 10 <dd><%= @note.content.name %></dd> 11 <dt><strong><%= model_class.human_attribute_name(:text) %>:</strong></dt> 12 <dd><%= @note.content.text %></dd> 13</dl> 14 15<%= link_to t('.back', :default => t("helpers.links.back")), 16 notes_path, :class => 'btn btn-default' %> 17<%= link_to t('.edit', :default => t("helpers.links.edit")), 18 edit_note_path(@note), :class => 'btn btn-default' %> 19<%= link_to t('.destroy', :default => t("helpers.links.destroy")), 20 note_path(@note), 21 :method => 'delete', 22 :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, 23 :class => 'btn btn-danger' %>
この様に書きました。noteのcontrollerには
ruby
1class NotesController < ApplicationController 2 before_action :set_note, only: [:show, :edit, :update, :destroy] 3 4 # GET /notes 5 # GET /notes.json 6 def index 7 @notes = Note.all 8 end 9 10 def mypage 11 @notes = Note.all 12 end 13 14 # GET /notes/1 15 # GET /notes/1.json 16 def show 17 @content = Content.find_by(note_id: params[:note_id]) 18 @note = Note.find(params[:id]) 19 end 20 21 # GET /notes/new 22 def new 23 @note = Note.new 24 @note.contents.build 25 26 end 27 28 # GET /notes/1/edit 29 def edit 30 @note = Note.find(params[:id]) 31 end 32 33 # POST /notes 34 # POST /notes.json 35 def create 36 @note = Note.new(note_params) 37 38 respond_to do |format| 39 if @note.save 40 format.html { redirect_to @note, notice: 'Note was successfully created.' } 41 format.json { render :show, status: :created, location: @note } 42 else 43 format.html { render :new } 44 format.json { render json: @note.errors, status: :unprocessable_entity } 45 end 46 end 47 end 48 49 # PATCH/PUT /notes/1 50 # PATCH/PUT /notes/1.json 51 def update 52 @note = Note.find(params[:id]) 53 respond_to do |format| 54 if @article.update(article_params) 55 format.html { redirect_to @article, notice: 'Article was successfully updated.' } 56 format.json { render :show, status: :ok, location: @article } 57 else 58 format.html { render :edit } 59 format.json { render json: @article.errors, status: :unprocessable_entity } 60 end 61 end 62 end 63 64 # DELETE /notes/1 65 # DELETE /notes/1.json 66 def destroy 67 @note = Note.find(params[:id]) 68respond_to do |format| 69 format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' } 70 format.json { head :no_content } 71 end 72 end 73 74 private 75 # Use callbacks to share common setup or constraints between actions. 76 def set_note 77 @note = Note.find(params[:id]) 78 end 79 80 # Never trust parameters from the scary internet, only allow the white list through. 81 def note_params 82 params.require(:note).permit( 83 :title 84 contents_attributes: [:id, :name, :text, :note_id] 85 ) 86 end 87end
回答1件
あなたの回答
tips
プレビュー