【发布时间】:2014-09-22 23:55:13
【问题描述】:
我一直在尝试构建一个提供打折假期旅行的应用程序,例如: (1)用户(旅行社)可以结合酒店(连锁酒店)和城市组成旅行 (2) 用户(普通用户)可以查看他/她已经访问过的酒店和城市。 (3) 另一个用户可以评估该交易相对于旅行社将为其住宿的国家和酒店的好坏程度。
模型看起来像这样
class User < ActiveRecord::Base
has_many :trips
has_many :reviews
end
class Trip < ActiveRecord::Base
belongs_to :user
belongs_to :hotel
belongs_to :city
end
class Hotel < ActiveRecord::Base
belongs_to :city
has_many :reviews, as: :reviewable
end
class City < ActiveRecord::Base
has_many :hotels
has_many :reviews, as: :reviewabel
end
class Review < ActiveRecord::Base
belongs_to :user
belongs_to :reviewable, polymorphic: true
end
问题是我可以弄清楚如何为 Hotel 和 City 创建控制器,因为它们只是在临时旅行的上下文中创建的。我检查了嵌套表单上的 rails casts 和 accepts_nested_attributes_for 的使用,但我似乎无法正确处理。
注意:我之所以将酒店和城市分开是为了能够独立检索评论。也就是说,我很享受在多伦多的四季酒店住宿,但不是纽约的四季酒店。 - 因为城市/酒店(=> 适应我不喜欢它的情况,因为酒店很垃圾,而我不喜欢它的情况是因为城市是)
注意 2:我知道在这个例子中将酒店和城市分开没有多大意义 - 我在自指定教程时犯了一个错误。但是这个问题一直困扰着我,如果是外卖订单而不是主菜/餐点/晚餐而不是酒店和城市,或者连锁餐厅和社区。p>
感谢任何帮助。谢谢
编辑
在 Settheline 的评论后编辑。
我的意思是城市和酒店的创建操作仅存在于旅行创建操作的上下文中。 Trip 有 2 个属性:标题和描述:只有这样我才“记录”行程。这是我的控制器的外观,可让您更好地了解
class TripsController < ApplicationController
before_action :signed_in_user
def show
@trip = Trip.find(params[:id])
end
def index
@trips = current_user.Trip.all
end
def new
@trip = Trip.new
end
def create
# @trip = Trip.new(trip_params)
@trip = current_user.trips.build(trip_params)
if @trip.save
flash[:success] = "Your trip was successfully published!"
redirect_to @trip
else
render 'new'
end
end
def edit
end
def update
if @trip.update_attributes(trip_params)
flash[:success] = "Trip was updated"
redirect_to @trip
else
render 'edit'
end
end
def destroy
Trip.find(params[:id]).destroy
flash[:success] = "trip was deleted. Thank you"
redirect_to @user #root_url
end
private
def trip_params
params.require(:trip).permit(:title, :description)
end
end
class CitiesController < ApplicationController
before_action :signed_in_user
def create
@city = City.new(city_params)
if @city.save
# flash[:success] = ""
else
render 'new'
end
end
# def destroy
# City.find(params[:id]).destroy
# flash[:success] = “City was deleted."
# redirect_to root_url
# end
private
def city_params
params.require(:city).permit(:name, :province, :country)
end
end
class HotelsController < ApplicationController
before_action :signed_in_user
def create
#similar to city
end
def destroy
#similar to city
end
private
def hotel_params
params.require(:hotel).permit(:name, :address,
:management_contact,
:city_id)
end
end
这就是问题所在: 我想在行程中添加/添加创建表单
sample_app/app/views/trips/new.html.erb
<% provide(:title, 'New Trip') %>
<h1>New Trip</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@trip) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_field :title, placeholder: "Type in a title" %>
<%= f.text_field :description, placeholder: "Any additional info." %>
<%= f.submit "Publish", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
【问题讨论】:
-
你不能创建控制器?喜欢
rails g controller Hotels?不完全理解您面临的问题 - 您能解释一下吗? -
我用我的视图和控制器编辑了原始帖子。如果仍不清楚,请告诉我,感谢您抽出宝贵时间提供帮助。
标签: ruby-on-rails activerecord ruby-on-rails-4 nested-attributes controllers