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

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

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

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

Ruby on Rails

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

Q&A

解決済

2回答

1111閲覧

ほrailsでnewだと動くformがeditだとエラーが出る

joru

総合スコア45

Ruby

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

Ruby on Rails

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

0グッド

2クリップ

投稿2019/04/22 06:57

編集2019/04/22 08:25

前提・実現したいこと

profileとcategoryもモデルが多対多の関係の際に、profileを介して、category_profile(中間テーブル)にデータを保存するformを作成中です。

ですが、newだとこれでうまくいくのですがeditができません。修正方法がわかる方、ぜひご指導いただけると幸いです。

発生している問題・エラーメッセージ

NameError in Profiles#edit undefined local variable or method `form' for #<#<Class:0x00007f9b011d84d8>:0x00007f9b011f3378> Did you mean? for fork

該当のソースコード

ruby

1profiles_controller.rb 2 3class ProfilesController < ApplicationController 4 before_action :set_profile, only: [:show, :edit, :update, :destroy] 5 6 # GET /profiles/new 7 def new 8 @profile = Profile.new 9 end 10 11 # GET /profiles/1/edit 12 def edit 13 end 14 15 # POST /profiles 16 # POST /profiles.json 17 def create 18 @profile = Profile.new(profile_params) 19 @profile.user_id = current_user.id 20 21 p @profile 22 23 respond_to do |format| 24 if @profile.save 25 format.html { redirect_to @profile, notice: 'Profile was successfully created.' } 26 format.json { render :show, status: :created, location: @profile } 27 else 28 format.html { render :new } 29 format.json { render json: @profile.errors, status: :unprocessable_entity } 30 end 31 end 32 end 33 34 # PATCH/PUT /profiles/1 35 # PATCH/PUT /profiles/1.json 36 def update 37 @profile = Profile.find(params[:id]) 38 @profile.user_id = current_user.id 39 40 respond_to do |format| 41 if @profile.update(profile_params) 42 format.html { redirect_to @profile, notice: 'Profile was successfully updated.' } 43 format.json { render :show, status: :ok, location: @profile } 44 else 45 format.html { render :edit } 46 format.json { render json: @profile.errors, status: :unprocessable_entity } 47 end 48 end 49 end

ruby

1new.html.erb 2 3<%= render 'form', profile: @profile %> 4 5<%= link_to 'Back', profiles_path %>

ruby

1edit.html.erb 2 3<%= render 'form', profile: @profile %> 4 5<%= link_to 'Show', @profile %> | 6<%= link_to 'Back', profiles_path %>

html

1_category_profile_fields.html.erb 2 3<tr> 4 <%= form.hidden_field :id %> 5 <td> 6 <%= form.collection_select :category_id, form.object.selectable_categories, :id, :name, {}, class: 'form-control' %> 7 </td> 8 <td> 9 <%= link_to_remove_association '削除', form %> 10 </td> 11</tr>

html

1_form.html.erb 2 3<%= form_with(model: profile, local: true) do |form| %> 4 <% if profile.errors.any? %> 5 <div id="error_explanation"> 6 <h2><%= pluralize(profile.errors.count, "error") %> prohibited this profile from being saved:</h2> 7 8 <ul> 9 <% profile.errors.full_messages.each do |message| %> 10 <li><%= message %></li> 11 <% end %> 12 </ul> 13 </div> 14 <% end %> 15 16 <div> 17 <div> 18 <%= link_to_add_association 'カテゴリーを追加', form, :category_profiles , 19 data: { 20 association_insertion_node: '#detail-association-insertion-point', 21 association_insertion_method: 'append' } 22 %> 23 </div> 24 <table> 25 <thead> 26 <tr> 27 <th> 28 </th> 29 <th> 30 </th> 31 </tr> 32 </thead> 33 <tbody id='detail-association-insertion-point'> 34 <div> 35 <%= form.fields_for :category_profiles do |form| %> 36 <%= render 'category_profile_fields', form: form %> 37 <% end %> 38 </div> 39 </tbody> 40 </table> 41 </div> 42 43 <div class="actions"> 44 <%= form.submit %> 45 </div> 46<% end %>

補足

Extracted source (around line #2):

1 <tr>
2 <%= form.hidden_field :id %>
3 <td>
4 <%= form.collection_select :category_id, form.object.selectable_categories,5 :id, :name, {}, class: 'form-control' %>
5 </td>
6 <td>

Trace of template inclusion: app/views/profiles/_form.html.erb, app/views/profiles/edit.html.erb

Rails.root: /Users/koheiobata/PORME/code/fukurikun

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

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

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

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

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

mather

2019/04/22 08:21

エラーの文面はこれで全部ですか?ファイルとかコードの行数がもし書かれていたら記載してください。
joru

2019/04/22 08:25

エラーの文面にかんして補足させていただきました
mather

2019/04/22 15:06

form.hidden_field :id などの部分が示された _form.html.erb にはありませんよね…。 別のファイルを調べてしまってるんじゃないですか? app/views/profiles/_form.html.erb こちらのファイルを見せてください。
mather

2019/04/22 15:10

あ、僕が読み間違えてますね。 _formの方ではなくて _category_profile_fields.html.erb を表示しようとしてエラーになってるんですね。
guest

回答2

0

自己解決

やり方を変えてprofileモデルでcategory_idsを保存する形でフォームを作ったところうまくいくようになりました。

みなさまご指導ありがとうございました。

投稿2019/04/23 04:58

joru

総合スコア45

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

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

0

accepts_nested_attributes_forの設定をモデルにしていますか?

erb

1 <%= form.fields_for :category_profiles do |form| %> 2 <%= render 'category_profile_fields', form: form %> 3 <% end %>

あと、問題ないと思いますが、formの変数が重複しているので、do |form|formを適当な名前に変えたほうが良いと思います。

投稿2019/04/22 09:33

odyu

総合スコア548

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問