前提・実現したいこと
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
回答2件
あなたの回答
tips
プレビュー