我尝试跟随http://guides.rubyonrails.org/form_helpers.html#nested-forms,因为它非常接近我要创建的内容。我目前正在使用Rails 4.2 beta。我真的很擅长轨道,所以我不知道我哪里出错了
控制器:
class UsersController < ApplicationController
def new
@user = User.new
@user.contact_requests.build
end
def create
@user = User.new(user_params)
end
private
def user_params
# strong_parameters, which requires us to tell Rails exactly which parameters
# we want to accept in our controllers
params.require(:user).permit(:first_name, :last_name, :email, :phone_number, contact_requests_attributes: [:message])
# params.require(:users).permit!
end
end
模型:
class User < ActiveRecord::Base
# before_save { |user| user.email = user.email.downcase }
has_many :contact_requests
# validate the presence of the attributes
validates(:first_name, presence: true)
validates(:last_name, presence: true)
validates(:email, presence: true)
accepts_nested_attributes_for :contact_requests
end
class ContactRequest < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
validates :message, presence: true, length: { maximum: 500 }
end
形式:
<section id="contact">
<div class="container">
<div class="title-container">Contact Us</div>
<div class="title-caption">Reach us at (415)-911-9999</div>
<%= form_for(@user, remote: true, id: "contact-form", class: "contact-input") do |f| %>
<div class="col-md-12">
<div class="col-md-6">
<div class="contact-input-margin form-group">
<%= f.text_field(:first_name, class: "form-control", placeholder: "First name")%>
</div>
<div class="contact-input-margin form-group">
<%= f.text_field(:last_name, class: "form-control", placeholder: "Last name") %>
</div>
<div class="contact-input-margin form-group">
<%= f.email_field(:email, class: "form-control", placeholder: "Email") %>
</div>
<div class="contact-input-margin form-group">
<%= f.telephone_field(:phone_number, class: "form-control", placeholder: "Phone number") %>
</div>
</div>
<div class="contact-input-margin col-md-6">
<div class="form-group">
<%= f.fields_for :contact_requests do |builder| %>
<%= builder.text_area(:message, class: "form-control contact-margin", rows: "8", placeholder: "Message...") %>
<% end %>
</div>
</div>
</div>
<%= f.submit(class: "btn btn-xl") %>
<% end %>
</div>
</section>
终端输出:
Started POST "/test" for ::1 at 2014-12-03 00:30:09 -0800
Processing by UsersController#create as JS
Parameters: {"utf8"=>"✓", "user"=>{"first_name"=>"bradfffo", "last_name"=>"li", "email"=>"skdljfd@ksdjf", "phone_number"=>"123123", "contact_requests"=>{"message"=>"sdlkfjskdfdsf"}}, "commit"=>"Save User"}
Unpermitted parameter: contact_requests
Completed 500 Internal Server Error in 10ms
ActionView::MissingTemplate (Missing template users/create, application/create with {:locale=>[:en], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
* "/Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0/lib/action_dispatch/templates"
* "/Users/bli1/Development/RoR/Boothie/app/views"
):
actionview (4.2.0.beta2) lib/action_view/path_set.rb:46:in `find'
actionview (4.2.0.beta2) lib/action_view/lookup_context.rb:121:in `find'
actionview (4.2.0.beta2) lib/action_view/renderer/abstract_renderer.rb:18:in `find_template'
actionview (4.2.0.beta2) lib/action_view/renderer/template_renderer.rb:40:in `determine_template'
actionview (4.2.0.beta2) lib/action_view/renderer/template_renderer.rb:8:in `render'
actionview (4.2.0.beta2) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.0.beta2) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.0.beta2) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.0.beta2) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.0.beta2) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.0.beta2) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.0.beta2) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.0.beta2) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.0.beta2) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.0.beta2) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.2.0.beta2) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/bli1/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.2.0.beta2) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.0.beta2) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.2.0.beta2) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.2.0.beta2) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.0.beta2) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.2.0.beta2) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.0.beta2) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.0.beta2) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.0.beta2) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.0.beta2) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:114:in `call'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:114:in `call'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:231:in `block in halting'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:231:in `call'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:231:in `block in halting'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:231:in `call'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:231:in `block in halting'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:87:in `call'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:87:in `run_callbacks'
actionpack (4.2.0.beta2) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.0.beta2) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.0.beta2) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.2.0.beta2) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0.beta2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0.beta2) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.0.beta2) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.0.beta2) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.0.beta2) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.0.beta2) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.0.beta2) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.0.beta2) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.2.0.beta2) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.0.beta2) lib/action_controller/metal.rb:236:in `block in action'
actionpack (4.2.0.beta2) lib/action_dispatch/routing/route_set.rb:72:in `call'
actionpack (4.2.0.beta2) lib/action_dispatch/routing/route_set.rb:72:in `dispatch'
actionpack (4.2.0.beta2) lib/action_dispatch/routing/route_set.rb:41:in `serve'
actionpack (4.2.0.beta2) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.0.beta2) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.0.beta2) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.0.beta2) lib/action_dispatch/routing/route_set.rb:750:in `call'
rack (1.6.0.beta2) lib/rack/etag.rb:24:in `call'
rack (1.6.0.beta2) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.0.beta2) lib/rack/head.rb:13:in `call'
actionpack (4.2.0.beta2) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.0.beta2) lib/action_dispatch/middleware/flash.rb:257:in `call'
rack (1.6.0.beta2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.0.beta2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.0.beta2) lib/action_dispatch/middleware/cookies.rb:558:in `call'
activerecord (4.2.0.beta2) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.0.beta2) lib/active_record/connection_adapters/abstract/connection_pool.rb:645:in `call'
activerecord (4.2.0.beta2) lib/active_record/migration.rb:378:in `call'
actionpack (4.2.0.beta2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.0.beta2) lib/active_support/callbacks.rb:83:in `run_callbacks'
actionpack (4.2.0.beta2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.0.beta2) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.0.beta2) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
web-console (2.0.0) lib/action_dispatch/debug_exceptions.rb:18:in `middleware_call'
web-console (2.0.0) lib/action_dispatch/debug_exceptions.rb:13:in `call'
actionpack (4.2.0.beta2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.0.beta2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.0.beta2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.0.beta2) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.0.beta2) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.0.beta2) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.0.beta2) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.0.beta2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.0.beta2) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.0.beta2) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.0.beta2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.0.beta2) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0.beta2) lib/action_dispatch/middleware/static.rb:103:in `call'
rack (1.6.0.beta2) lib/rack/sendfile.rb:113:in `call'
railties (4.2.0.beta2) lib/rails/engine.rb:514:in `call'
railties (4.2.0.beta2) lib/rails/application.rb:161:in `call'
rack (1.6.0.beta2) lib/rack/lock.rb:17:in `call'
rack (1.6.0.beta2) lib/rack/content_length.rb:15:in `call'
rack (1.6.0.beta2) lib/rack/handler/webrick.rb:89:in `service'
/Users/bli1/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/Users/bli1/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/Users/bli1/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/missing_template.text.erb (0.3ms)
路线:
Rails.application.routes.draw do
root 'home#index'
match '/send_mail', to: "home#send_mail", via: "get"
get 'test', to: "users#new"
post 'test', to: "users#create"
答案 0 :(得分:3)
请勿忘记id
的{{1}}属性,您需要修改contact_requests
方法,见下文
user_params
我已将params.require(:user).permit(:first_name, :last_name, :email, :phone_number, contact_requests_attributes: [:id, :message])
属性添加到id
此外,您需要使用contact_requests_attributes
代替:user
,我已在上面更正过了!
试一试!
答案 1 :(得分:1)
您需要更改允许的参数
params.require(:user).permit(:first_name, :last_name, :email, :phone_number, contact_requests: {:id, :message})