无法使用关联表格从表单中创建记录

时间:2021-08-24 22:23:37

I'm new at Ruby on Rails and have been struggling through for a while now. Specifically my issue is that I have two tables: appointments and service1s. Service1 has_many :appointments and Appointment belongs_to :service1.

我是Ruby on Rails的新手,现在已经苦苦挣扎了一段时间。具体来说,我的问题是我有两个表:约会和service1s。 Service1 has_many:约会和约会belongs_to:service1。

On my site, users can sign up for an appointment. In that form they can select a service as such:

在我的网站上,用户可以注册预约。在那种形式下,他们可以选择这样的服务:

<%= f.label "Service" %>
  <%= f.select :service1, options_from_collection_for_select(Service1.all, :id, :name), :prompt => true %>

In the appointments controller I have: def create @stylists = ["Zoe Andreadis", "Amanda Giorgi", "Michelle Oda", "Danny Quaranta", "Demi Tsionis", "Christina Vicencio", "Nancianne Warren"]

在约会控制器中我有:def创建@stylists = [“Zoe Andreadis”,“Amanda Giorgi”,“Michelle Oda”,“Danny Quaranta”,“Demi Tsionis”,“Christina Vicencio”,“Nancianne Warren”]

 @appointment = Appointment.new(params.require(:appointment).permit(:date, :start, :stylistpref))
@appointment.service1 = Service1.find_by_name(:service1).id
@appointment.user = current_user
if @appointment.save

  flash[:success] = "Appointment Booked for " + @appointment.date + @appointment.time
  redirect_to(root_url)
else
  render 'new'
end

end

结束

The f.select stores the name as string, and I'm pretty sure the appointment model requires the id. With that code I get this error message on the @appointment.service1 line: undefined method `id' for nil:NilClass

f.select将名称存储为字符串,我很确定约会模型需要id。使用该代码,我在@ appointment.service1行上收到此错误消息:nil的未定义方法`id':NilClass

If i get rid of that line, then I get the flash error message that service1 was left blank.

如果我摆脱了那一行,那么我得到了flash1错误信息,即service1被留空了。

Please help me solve this! Thank you!

请帮帮我解决这个问题!谢谢!

1 个解决方案

#1


0  

You don't have a Service1 with the name 'service1'. find_by_name is coming back with nil

您没有名为“service1”的Service1。 find_by_name以nil回来

Update in response to your comment:

更新以回应您的评论:

That first code snippet you posted: that's within a form, correct? I think you want params[:object_key][:service1] where :object_key is the name of the record the form is based on.

您发布的第一个代码段:这是在表单中,对吗?我想你想要params [:object_key] [:service1]其中:object_key是表单所基于的记录的名称。

I'm pretty sure there's an easier way to do what you're doing though. I'd have to see more code though.

我很确定有一种更简单的方法来做你正在做的事情。我必须看到更多的代码。

#1


0  

You don't have a Service1 with the name 'service1'. find_by_name is coming back with nil

您没有名为“service1”的Service1。 find_by_name以nil回来

Update in response to your comment:

更新以回应您的评论:

That first code snippet you posted: that's within a form, correct? I think you want params[:object_key][:service1] where :object_key is the name of the record the form is based on.

您发布的第一个代码段:这是在表单中,对吗?我想你想要params [:object_key] [:service1]其中:object_key是表单所基于的记录的名称。

I'm pretty sure there's an easier way to do what you're doing though. I'd have to see more code though.

我很确定有一种更简单的方法来做你正在做的事情。我必须看到更多的代码。