Katie Le
Katie Le

Reputation: 19

Rails form_for nested forms not creating/saving

Here is my models:

class Drama < ApplicationRecord
has_many :reviews 
has_many :users, :through => :reviews
validates :name, uniqueness: true 
accepts_nested_attributes_for :reviews
accepts_nested_attributes_for :users 

end

class Review < ApplicationRecord
belongs_to :user 
belongs_to :drama 

end

class User < ApplicationRecord has_many :reviews has_many :dramas, :through => :reviews validates :username, uniqueness: true, presence: true validates :email, uniqueness: true, presence: true has_secure_password end

Here is the nested form that just isn't saving/creating!

 <div>
<%= form_for @drama do |f| %>

<h2>Drama Info</h2>

<strong>Name: <%= f.text_field :name %></strong><br>
<strong>Genre: <%= f.text_field :genre %></strong><br>


<h2>Review Info</h2>

<%= f.fields_for :reviews, @drama.reviews.build do |r| %>
<%= r.label :title %>
<%= r.text_field :title %><br>

<%= r.label :rating %>
<%= r.number_field :rating %><br>

<%= r.label :content %>
<%= r.text_area :content %><br>
<%end%>

<%= f.submit %>
<%end%>
</div>

Here are my routes:

 Rails.application.routes.draw do

  resources :dramas do
    resources :reviews, only: [:new, :show]
  end

  resources :dramas 
  resources :users
  resources :reviews 

  get '/auth/facebook/callback', to: 'sessions#create_with_fb'

  get '/', to: 'sessions#home'
  get '/signup', to: 'users#new'
  post '/signup', to: 'users#create'
  get '/login', to: 'sessions#new'
  post '/login', to: 'sessions#create'
  post '/logout', to: 'sessions#destroy'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

Here is my dramas controller:

class DramasController < ApplicationController
before_action :find_by_drama, only: [:edit, :update, :destroy, :show]
before_action :current_user, only: [:create, :update, :edit, :destroy]
def index 
    @dramas = Drama.all 
end 

def new    
    @drama = Drama.new 
    @drama.reviews.build 
end  


def show 
    session[:drama_id] = @drama.id
   # @drama = Drama.find(session[:drama_id])
end 

def create 
    @drama = Drama.new(drama_params)
    if @drama.save 
        redirect_to dramas_path
    else 
        render :new 
end 

end

def update 
    @drama = Drama.update(drama_params)
    redirect_to dramas_path 
end 

def edit 
end 

def destroy 
    @drama.destroy 
    redirect_to dramas_path 
end 


private 

def drama_params
    params.require(:drama).permit(
        :name,
        :genre,
        reviews_attributes: [
            :title,
            :rating,
            :content
          ]
    )
end 

end

My db. schema :

ActiveRecord::Schema.define(version: 2020_12_19_073757) do

  create_table "dramas", force: :cascade do |t|
    t.string "name"
    t.string "genre"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "reviews", force: :cascade do |t|
    t.integer "user_id"
    t.integer "drama_id"
    t.string "title"
    t.text "content"
    t.integer "rating"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "users", force: :cascade do |t|
    t.string "username"
    t.string "email"
    t.string "password_digest"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

end

Please help! Been stuck

Upvotes: 0

Views: 59

Answers (1)

Tashows
Tashows

Reputation: 559

The @drama.reviews.build part inside fields_for is not needed since you already built the review in the new action. Try removing it and see if it works. Then in the edit action add @drama.reviews.build if you want to build another review in that action as well. Building nested attributes in the view can lead to some funky behavior. If it still doesn't work post a trace from the running app or try @drama.errors.inspect either in the view or use puts to print errors in the server logs.

Upvotes: 0

Related Questions