问题描述
我是一名 Rails 学生,正在寻求有关嵌套表单的帮助。我的项目由四个模型组成:Users、Trips(想想:行程)和 Eatery,以及 EateryTrips,它将 Trips 和 Eateries 作为连接模型连接起来,具有双向多通道关系。
我正在尝试向我的嵌套表单添加一个集合选择(在添加集合选择之前效果很好)。我实际上不确定收藏精选的最佳地点在哪里。
这是我的上下文模型:
class User < ApplicationRecord
has_many :trips,dependent: :destroy
validates :name,:presence => true
validates :email,:uniqueness => true
validates :email,email: true
has_secure_password
end
class EateryTrip < ApplicationRecord
belongs_to :eatery
belongs_to :trip
validates_uniqueness_of :eatery_id,scope: [:trip_id]
accepts_nested_attributes_for :eatery,allow_destroy: true
scope :visited,-> { where("visit_date < ?",Time.Now ) }
end
class Eatery < ApplicationRecord
has_many :eatery_trips
has_many :trips,through: :eatery_trips
validates :city,:presence => true
validates :state,:presence => true
validates :name,:presence => true
validates :about,:presence => true
validates :food_categories,:presence => true
validates :dishes,:presence => true
end
class Trip < ApplicationRecord
has_many :eatery_trips
has_many :eateries,through: :eatery_trips
validates :title,:presence => true
validates :description,:presence => true
accepts_nested_attributes_for :eatery_trips,allow_destroy: true
end
我的行程控制器目前如下所示:
class TripsController < ApplicationController
before_action :verify_user,except: [:show,:index]
before_action :find_user_by_id,except: [:new,:index]
before_action :find_trip_by_id,only: [:show,:edit,:update]
def new
@user = current_user
@trip = @user.trips.build
# 3.times { @trip.eatery_trips.build }
3.times { @trip.eatery_trips.build.build_eatery }
end
def create
@trip = @user.trips.new(trip_params)
if @trip.save
redirect_to user_trip_path(@user,@trip)
else
render :new
end
end
Trips 控制器中的强参数如下所示:
def trip_params
params.require(:trip).permit(:title,:description,:eatery_trips_attributes => [:id,:visit_date,:review,:eatery_id,:eatery_attributes => [:city,:state,:name,:about,:food_categories,:dishes,:id]])
end
我创建新的嵌套表单:
<% require 'pry' %>
<%= form_for [@user,@trip] do |trip_form| %>
<% if @trip.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@trip.errors.count,"error") %> prohibited this post from being saved:</h2>
<% @trip.errors.full_messages.each do |message| %>
<ul>
<li><%= message %></li>
</ul>
<% end %>
</div>
<% end %>
<div id="trip">
<%= trip_form.label :itinerary_title %>
<%= trip_form.text_area :title,placeholder: "Ex: Brunch and Breweries in Savannah",size: "50%" %> <br /><br />
<%= trip_form.label :description %>
<%= trip_form.text_area :description,placeholder: "Ex: Southern Road Trip 2021",size: "50%" %><br /><br />
</div>
<div id="eatery_trip_attributes">
<%= trip_form.fields_for :eatery_trips do |eatery_trip_form| %>
<%= eatery_trip_form.label :intended_visit_date %>
<%= eatery_trip_form.date_field :visit_date %><br /><br />
<%= eatery_trip_form.label :review_after_meal %>
<%= eatery_trip_form.text_area :review,placeholder: "Add notes here during or after your road trip." %><br /><br />
<h2>Select existing eatery or create a new one below.</h2>
<%= eatery_trip_form.collection_select(:eatery_id,Eatery.all,:id,prompt: true) %> <br /><br />
<div id="eatery_attributes">
<%= eatery_trip_form.fields_for :eatery do |eatery_form| %>
<ul>
<%= eatery_form.label :city %>
<%= eatery_form.text_field :city,placeholder: "Ex: Savannah" %><br /><br />
<%= eatery_form.label :state %>
<%= eatery_form.text_field :state,placeholder: "Ex: GA" %><br /><br />
<%= eatery_form.label :name %>
<%= eatery_form.text_field :name,placeholder: "Cotton & Rye" %><br /><br />
<%= eatery_form.label :about %>
<%= eatery_form.text_field :about,placeholder: "Pro tip: Book a private dinner inside the original bank vault.",size: "50%" %><br /><br />
<%= eatery_form.label :food_categories %>
<%= eatery_form.text_field :food_categories,placeholder: "Southern food,dinner,dessert",size: "50%" %><br /><br />
<%= eatery_form.label :dishes %>
<%= eatery_form.text_field :dishes,placeholder: "Crispy chicken wings,peanut butter pie",size: "50%" %><br /><br />
</ul>
</div>
<% end %>
<% end %>
</div>
<%= render "submit_button" %>
<% end %>
到目前为止,它出现在正确的位置,我可以看到下拉列表中添加的所有餐馆。
问题: 我如何为用户添加从 collection_select 下拉菜单中选择餐馆或在嵌套表单中创建新餐馆的选项? 我是否应该关闭对这些属性的验证,以便在我从下拉列表中选择时不会出错?谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)