我必须提交多个表单,我遵循了这篇文章的建议:如何从 Rails 中的同一页面提交多个重复的表单 - 最好是一键
I have to submit multiple forms, I followed the advice of this post: How to submit multiple, duplicate forms from same page in Rails - preferably with one button
注意我对 Rails/编程还是很陌生,我的一些做事方式可能并不理想.
Note I'm still quite new to Rails/programming and some of my ways of doing things might not be ideal.
这是我的观点:
= form_tag ([@event, @registration]) do - x.times do = render 'multi_form' = submit_tag "Submit registrations"表单(注意字段较多):
The form (note that there are more fields):
- hidden_field_tag :event_id, :value => @event.id .control-group = label_tag :title .controls = select("registrations[][title]", :registration, Registration::TITLE) .control-group = label_tag :first_name .controls = text_field_tag "registrations[][first_name]" .control-group = label_tag :last_name .controls = text_field_tag "registrations[][last_name]" .control-group = label_tag :email .controls = text_field_tag "registrations[][email]"控制器:
def create array_number = 0 x.times do @registration = Registration.new(params[:registrations][array_number]) @registration.save UserMailer.registration_user_notify(@event, @registration).deliver array_number = array_number + 1 end respond_to do |format| format.html {redirect_to thank_you_event_registrations_path(@event)} end end提交时,在某种程度上,它似乎是在做正确的事情,因为它会向 x 个唯一的电子邮件地址发送一封电子邮件,这让我认为 @registration 在每个循环中都包含正确的详细信息 - 它没有保存然而到数据库.我可以看到所有的参数都在日志文件中,除了 :title 似乎在做一些不好的事情(见下文:但我稍后会关注),我现在想要它做的主要事情是运行每个数组并将其另存为一个新条目.
When submitting it seems, to an extent, to be doing the right thing, for one it fires off an email to x unique email addresses, which makes me think that @registration contains the correct details in each loop - it's not saving to the database however. I can see that all the params are there in the log file, except that :title seems to be doing something bad (see below: but I'll focus on that later), the main thing I want it to do now is run though each array and save it as a new entry.
日志:
Parameters: {"utf8"=>"â", "authenticity_token"=>"BQXm5fngW27z/3Wxy9qEzu6D8/g9YQIfBL+mFKVplgE=", "event_id"=>"7", "registrations"=>[{"title"=>{"registration"=>"Mrs"}, "first_name"=>"Person", "last_name"=>"One", "email"=>"charl@privatelabel.co.za"...我希望我提供的信息足够了,任何建议将不胜感激.
I'm hoping the info I provided is enough, any advice will be appreciated.
谢谢!
@iblue
它成功了!这是一个验证错误,它将所有内容保存到不同的行中.非常感谢!
It did the trick! It was a validation error and it's saving everything into different rows. Thank you very much!
如果可以的话,还有一件事,知道应该如何格式化 :title 表单部分以返回参数:
One more thing if I may, any idea how the :title form part should be formatted in order to return paramater:
"title"=>"Mrs",相反:
"registrations"=>[{"title"=>{"registration"=>"Mrs"},再次感谢!
推荐答案您不是在检查 @registration.save 是否确实保存了记录.它可以返回 true 或 false.我想它只是默默地失败了.
You are not checking if @registration.save actually saves the record. It can return true or false. I guess it just silently fails.
如果您使用 @registration.save!,它会在出现任何问题时引发异常.我猜那里存在某种验证错误.
If you use @registration.save!, it wile raise an exception when anything goes wrong. I guess there is some kind of validation error there.
更多推荐
在 Rails 中提交多个表单
发布评论