[英]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 class 用户 < 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:这是我的戏剧 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
被卡住了
The @drama.reviews.build
part inside fields_for
is not needed since you already built the review in the new
action.不需要
fields_for
中的@drama.reviews.build
部分,因为您已经在new
操作中构建了评论。 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.然后在
edit
操作中添加@drama.reviews.build
如果您还想在该操作中构建另一个评论。 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.如果它仍然不起作用,请从正在运行的应用程序发布跟踪或在视图中尝试
@drama.errors.inspect
或使用puts
在服务器日志中打印错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.