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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

1回答

3609閲覧

railsで、ネストされたモデル(1対多の関係を持つモデル)で1に多のモデルを表示方法

shinogi1217

総合スコア61

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2015/08/09 06:26

編集2015/08/09 12:00

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

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

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

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

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

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

rifuch

2015/08/09 08:47

NoteControllerとshowのビューのソースをのせてみてくれませんか?
guest

回答1

0

ベストアンサー

モデルクラスでhas_many宣言しているので、普通に関連でとれます。
表示の部分では、has_many宣言しているので、同名のメソッドを呼んでループしてあげればOK

Ruby

1# NoteController 2 3def show 4 @note = Note.find(params[:id]) 5end

html.erb

1<!-- noteの表示に関しては省略 --> 2<% @note.contents.each do |content| %> 3 <dt><strong><%= Content.human_attribute_name(:text) %>:</strong></dt> 4 <dd><%= content.text %></dd> 5<% end %>

提示いただいたコードからは推察できませんが、
エラーの原因は、多分、note_idパラメータが渡っていないのにそれを利用しようとしたからではないかと。

投稿2015/08/09 22:32

編集2015/08/09 22:33
rifuch

総合スコア1901

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

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

shinogi1217

2015/08/10 04:46 編集

ありがとうございます! 解決いたしました! showのviewsに <% @note.contents.each do |content| %> を追加したら呼び出すことに成功しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問