2012-10-13 98 views
0

我有兩個模型:userprofil e,我想使用相同的表單來向它們輸入數據。Ruby on Rails:處理屬於不同模型的表單

我在user視圖和控制器內工作。我有一個工作表user colums。 但是,當涉及到配置文件模型時,如何從用戶視圖訪問它?

user.rb:

class User < ActiveRecord::Base 
    has_one :profile     
#....# 
end 

profile.rb:

class Profile < ActiveRecord::Base 
    # attr_accessible :title, :body 
    belongs_to :user 
end 

用戶/ new.html.erb:

<%= form_for @profile do |f| %> 
    <% if @profile.errors.any? %> 
    <div class="error_messages"> 
     <h2>Form is invalid</h2> 
     <ul> 
     <% for message in @profile.errors.full_messages %> 
      <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 
     <%= f.label :height, "My height is: (cm)" %><%= f.select :height, options_for_select(140..220) %><br> 
     <%= f.submit "Next" %> 
<% end %> 

當我這樣做,我得到以下錯誤消息:

undefined method `model_name' for NilClass:Class 

編輯:

class ApplicationController < ActionController::Base 
    protect_from_forgery 

private 

def current_user 
    @current_user ||= User.find(session[:user_id]) if session[:user_id] 
end 
helper_method :current_user 
end 

跟蹤針對零未定義的方法`個人資料」:NilClass

app/controllers/users_controller.rb:27:in `new' 
actionpack (3.2.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action' 
actionpack (3.2.8) lib/abstract_controller/base.rb:167:in `process_action' 
actionpack (3.2.8) lib/action_controller/metal/rendering.rb:10:in `process_action' 
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:18:in `block in process_action' 
activesupport (3.2.8) lib/active_support/callbacks.rb:414:in `_run__4507413321759690622__process_action__4445552126419236557__callbacks' 
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback' 
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks' 
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks' 
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:17:in `process_action' 
actionpack (3.2.8) lib/action_controller/metal/rescue.rb:29:in `process_action' 
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action' 
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `block in instrument' 
activesupport (3.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `instrument' 
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:29:in `process_action' 
actionpack (3.2.8) lib/action_controller/metal/params_wrapper.rb:207:in `process_action' 
activerecord (3.2.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action' 
actionpack (3.2.8) lib/abstract_controller/base.rb:121:in `process' 
actionpack (3.2.8) lib/abstract_controller/rendering.rb:45:in `process' 
actionpack (3.2.8) lib/action_controller/metal.rb:203:in `dispatch' 
actionpack (3.2.8) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' 
actionpack (3.2.8) lib/action_controller/metal.rb:246:in `block in action' 
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `call' 
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `dispatch' 
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:36:in `call' 
journey (1.0.4) lib/journey/router.rb:68:in `block in call' 
journey (1.0.4) lib/journey/router.rb:56:in `each' 
journey (1.0.4) lib/journey/router.rb:56:in `call' 
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:600:in `call' 
actionpack (3.2.8) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' 
rack (1.4.1) lib/rack/etag.rb:23:in `call' 
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call' 
actionpack (3.2.8) lib/action_dispatch/middleware/head.rb:14:in `call' 
actionpack (3.2.8) lib/action_dispatch/middleware/params_parser.rb:21:in `call' 
actionpack (3.2.8) lib/action_dispatch/middleware/flash.rb:242:in `call' 
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context' 
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call' 
actionpack (3.2.8) lib/action_dispatch/middleware/cookies.rb:339:in `call' 
activerecord (3.2.8) lib/active_record/query_cache.rb:64:in `call' 
activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call' 
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call' 
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `_run__4338139503461093137__call__2795110861990608974__callbacks' 
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback' 
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_call_callbacks' 
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks' 
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call' 
actionpack (3.2.8) lib/action_dispatch/middleware/reloader.rb:65:in `call' 
actionpack (3.2.8) lib/action_dispatch/middleware/remote_ip.rb:31:in `call' 
actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call' 
actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' 
railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app' 
railties (3.2.8) lib/rails/rack/logger.rb:16:in `call' 
actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call' 
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call' 
rack (1.4.1) lib/rack/runtime.rb:17:in `call' 
activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call' 
rack (1.4.1) lib/rack/lock.rb:15:in `call' 
actionpack (3.2.8) lib/action_dispatch/middleware/static.rb:62:in `call' 
railties (3.2.8) lib/rails/engine.rb:479:in `call' 
railties (3.2.8) lib/rails/application.rb:223:in `call' 
rack (1.4.1) lib/rack/content_length.rb:14:in `call' 
railties (3.2.8) lib/rails/rack/log_tailer.rb:17:in `call' 
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service' 
/home/tim/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' 
/home/tim/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' 
/home/tim/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' 
+0

請告訴我們你寫在你的控制器是什麼..have你在用戶控制器中定義@profile .. –

回答

0

在呈現該視圖控制器,你需要在該配置文件對象傳遞查看使用。這可能與將@profile = @user.profile置於控制器的new方法中一樣簡單。

+0

當我這樣做時,我得到以下錯誤消息:未定義的方法'配置文件'爲零:NilClass – TimmyOnRails

+0

那麼也許這不是正確的方法。 :)你有沒有添加@profile的東西你的看法正常情況下工作? – dpassage

+0

是的,我有一個窗體的@user視圖,並且工作得很好。我不瞭解的是如何通過用戶視圖填充配置文件模型。 – TimmyOnRails

2

你需要做這樣的事情,

<% form_for [current_user, @profile] |f| do %> 
    #define the fields here 
<% end %> 

但在users_controller - 新動作,你需要做這樣的事情

def new 
    @profile = current_user.profile.new 
end 
+0

我仍然得到相同的錯誤信息:'未定義的方法'配置文件爲零:NilClass'。我添加了一個'profiles'控制器和標準的'new'動作,但它似乎沒有幫助 – TimmyOnRails

+0

你可以使用調試器並檢查current_user是否爲零?如果你可以在這裏打印你的痕跡將是有幫助的 –

+0

我已經添加了一些更多的細節,這是你要求的嗎? – TimmyOnRails