Ruby on rails 在一个表单中处理不同的模型
我一直在尝试构建一个提供折扣度假旅行的应用程序,以便: 1用户旅行社可以通过结合酒店、连锁酒店和城市来组成旅行 2用户常规用户可以查看他/她已经访问过的酒店和城市。 3另一个用户可以评估旅行社将为其提供住宿的国家/地区和酒店方面的交易情况 模型看起来像这样 类用户Ruby on rails 在一个表单中处理不同的模型,ruby-on-rails,activerecord,ruby-on-rails-4,nested-attributes,controllers,Ruby On Rails,Activerecord,Ruby On Rails 4,Nested Attributes,Controllers,我一直在尝试构建一个提供折扣度假旅行的应用程序,以便: 1用户旅行社可以通过结合酒店、连锁酒店和城市来组成旅行 2用户常规用户可以查看他/她已经访问过的酒店和城市。 3另一个用户可以评估旅行社将为其提供住宿的国家/地区和酒店方面的交易情况 模型看起来像这样 类用户
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
问题是:
我想在trip one中创建/添加表单
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>
接受\u嵌套的\u属性\u允许您在关联上保存属性。虽然您的模型中确实存在关联,但这并不一定意味着您需要使用accepts\u嵌套的属性。这取决于代码如何流经控制器
简单例子
例如,您可能希望允许您的用户查看他们的旅行和评论。首先,您需要一个方法来获取当前用户:
用户\u controller.rb
此方法将由所有控制器继承,并允许它们获取当前用户。有很多解决方案可以获得当前用户,这肯定不是一个好的解决方案,但出于演示目的,它是可以的
旅行与评论
现在,您可以为当前用户创建一些控制器,以查看他们的行程和评论:
trips_controller.rb
查看\u controller.rb
现在您有了一些控制器操作,显示当前用户的行程/回顾。我认为这个示例演示了如何创建控制器,并且不一定需要接受嵌套的属性。您不能创建控制器吗?比如rails g控制器酒店?没有完全理解你面临的问题-你能解释一下吗?我用我的视图和控制器编辑了原始帖子。如果还不清楚,请告诉我,谢谢你花时间帮忙。很抱歉,我在原来的帖子里不够清楚。我已经编辑了这个问题以包含我的观点,希望现在更好。我在创建行程动作/表单时遇到问题,因为我不知道如何包含城市和酒店表单。你说的关于使用accepts_nested_attributes_的话让我想,我要检查一下rails的4向副本,我的模型中可能缺少了什么或者有错误。感谢您抽出时间回答。
class ApplicationController < ActionController::Base
def current_user
@current_user ||= User.find(session[:user_id])
end
end
class TripsController < ApplicationController
def index
@trips = current_user.trips.all
end
end
class ReviewsController < ApplicationController
def index
@reviews = current_user.reviews.all
end
end