如果我没有准确地传达我的求助请求,我表示歉意。
我想要完成的是在选择appointments
按钮后根据某些对象值将条目写入Book
表。此设置中没有要收集的表单值。如果要点击Book
按钮,它会收集tech.email
,service.name
的值以及service_type
的某些值,然后将这些值写入{{1}表。
细分
- 将
appointments
的值写入tech.email
表。- 将分配给
appointments
的{{1}}的值写入service.name
表格- 写入分配给特定的
醇>tech
的所有值 分配给特定appointments
的{{1}}service_type
表
简而言之,我如何捕获service
的实例变量,以便我可以在tech
视图中将其设置为appointments
,service_type
,{{1等等...?
所有这些都发生在appointment
如果我将@service_type.name
路径以菊花链形式连接,只是为了能够访问所有值。
@service_type.service.name
(我希望所有@service_type.service.tech
值都可访问)
Rails控制台
如果我在控制台中执行以下操作。tech_controller.rb
,我可以做一个首选 反向菊花链方法:
service_type
我有以下内容:
宝石
@service_type = tech.first.service.first.service_type.first.
service_type
appointmentments.rb模型(富连接表)
service_type = ServiceType.first
tech.rb模型(带有技术列表的数据库 - 通过@service_type.service.tech.email
gem注册)
devise: user signup
service.rb模型
cocoon: customizable nested objects
service_type.rb模型
class Appointment < ActiveRecord::Base
belongs_to :customer
belongs_to :tech
end
tech_controller.rb
devise
show.html.erb
class Tech < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :appointments #unsure is this is necessary since has_many :through below
has_many :customers, :through => :appointments
has_many :services
has_many :service_types
end
约会视图
class Service < ActiveRecord::Base
has_many :service_types
belongs_to :tech
accepts_nested_attributes_for :service_types, :reject_if => :all_blank, :allow_destroy => true
end
服务(包含带有服务主要类别列表的数据)
class ServiceType < ActiveRecord::Base
#nested objects generated by cocoon gem
belongs_to :service
belongs_to :tech #added 5/27 unsure if needed
end
class TechsController < ApplicationController
def index
@techs = Tech.all
@customer = Customer.all
end
def show
@tech = Tech.find(params[:id])
@services = @tech.services.all
end
def new
#@appointment = Appointment.new
end
def create
@appointment = Appointment.new(params[:id])
#@appointment.booked_service = @tech.first.services.first.name - the vars below are just a mickey mouse hacked up example of the value I'm trying to obtain.
@@appointment.booked_type = @tech.first.services.first.service_types.first.name
@@appointment.booked_price = @tech.first.services.first.service_types.price
#...etc
#...etc
#...etc
@appointment.save
redirect_to @appointment
end
def edit
end
def destroy
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def appointment_params
params.require(:appointment).permit(:booked_service, :booked_type, :booked_type_time_length, :booked_price, :customer_id, :tech_id)
#params.require(:appointment).permit(:booked_service => :service, :booked_type, :booked_type_time_length, :booked_price, :customer_id, :tech_id)
end
end
引用 <tbody>
<% @services.each do |service| %>
<tr>
<td><%= service.name %></td>
<td>
<table>
<tr>
<th>Name</th>
<th>Time</th>
<th>Price</th>
<th>Book Appointment</th>
</tr>
<%service.service_types.each do |service_type|%>
<tr>
<td><%= service_type.name %></td>
<td><%= service_type.time %></td>
<td><%= service_type.price %></td>
<td><%= button_to 'Book', action: 'create' %></td>
</tr>
<%end%>
</table>
</td>
</tr>
<% end %>
</tbody>
db
ServiceType(来自cocoon gem,允许我创建具有多个自定义属性的嵌套服务类型)
<h2>Thank you for booking your appointment!</h2>
<b>Service:</b> <%= @appointment.id %><br>
<b>Your Booked Service:</b> <%= @appointment.booked_service %><br>
<b>Type selected:</b> <%= @appointment.booked_type %><br>
<b>Length of service:</b> <%= @appointment.booked_type_time_length %><br>
<b>Service Price:</b> <%= @appointment.booked_price %><br>
<b>Service:</b> <%= @appointment.customer_id %><br>
<b>Your Tech:</b> <%= @appointment.tech_id %>
mysql> describe services;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| tech_id | int(11) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
引用tech_id
db
修改:点击按钮后添加错误 - 5/29 - 2:38 AM
返回的错误是:
tech
@service_type = Tech.find(params [:tech])
如果需要进一步的详细信息,请与我们联系。
任何帮助将不胜感激。 感谢