【问题标题】:Instantiating an associated model input is available to populate the model attributes (has_one)实例化关联模型输入可用于填充模型属性 (has_one)
【发布时间】:2023-03-31 06:24:01
【问题描述】:

创建数据模型对象以便创建表单的正确方法是什么。当我第一次创建对象时,它没有具有有效属性数据的参数数据,因此 build_ 返回一个无效对象并且 rails 抛出 500 Internal Server Error。

这是我正在查看的内容:

模型

class Credentials < ActiveRecord::Base
belongs_to user
...
class User < Active ::Base
has_one :credentials, :dependent => :destroy
...

控制器

def new
@cred = current_user.build_credentials 
    render 'edit'
end
    ...

查看

<%= form_for(@cred) do |f| %>

日志

Started GET "/credentials" for 127.0.0.1 at 2012-11-16 00:43:26 -0500
Processing by CredentialsController#new as HTML
  [1m[35mUser Load (0.3ms)[0m  SELECT `users`.* FROM `users` WHERE `users`.`remember_token` = 'oWGXC2dmcg2sRyg5mvA6fw' LIMIT 1
  [1m[36mUser Load (0.3ms)[0m  [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1[0m
  [1m[35mCredentials Load (0.4ms)[0m  SELECT `credentials`.* FROM `credentials` WHERE `credentials`.`user_id` = 1 LIMIT 1
  [1m[36m (0.1ms)[0m  [1mBEGIN[0m
  [1m[35m (0.1ms)[0m  COMMIT
  Rendered credentials/edit.html.erb within layouts/application (8.8ms)
Completed 500 Internal Server Error in 100ms

秒结果

undefined method `credentials_index_path' for #<#<Class:...

凭证模型有许多验证要求,因此new 创建的对象无效(我可以在控制台中确认这一点),因为填充的唯一值是外键,在本例中为 user_id。但是,在控制台中,我可以建立 @credentials 变量,并在下一个命令中分配所有属性。在 Web 服务器中,500 错误使得无法遵循相同的方法。

更新 为了更清楚地说明这一点,问题是@cred = current_user.build_credentials 返回一个零。 我在创建@cred 后直接调用raise @cred.inspect 以验证它的为零。当我看到 form_for(@cred)... @cred 只是一个 nil 对象时,它会与 undefined method 'model_name' for NilClass:Class 一起呕吐

【问题讨论】:

    标签: ruby-on-rails associations belongs-to has-one


    【解决方案1】:

    为什么凭据属于控制器?它应该属于用户,如果你使用has_one关联,你应该使用凭证的单数名词:

    型号

    class Credential < ActiveRecord::Base
    belongs_to user
    ...
    class User < Active ::Base
    has_one :credential, :dependent => :destroy
    

    控制器

    在你的控制器中,如果你想为表单创建新对象,你只需要:

    def new
      @cred = current_user.build_credential 
    end
    
    def create
      @cred = current_user.build_credential(params[:cred])
      if @cred.save
        # what you do when creating object success
      else
        # what you do when creating object failed
      end
    end
    

    查看

    在app/views/credentials/ 文件夹中的new.html.erb 中,创建您的表单:

    <%= form_for @cred do |f| %>
    ...
    <% end %>
    

    要链接到new.html.erb 页面,请在您的视图中创建一个link_to:

    <%= link_to 'New Credential', new_credential_path %>
    

    路线

    如果你想使用 RESTful 路由,在你的 routes.rb 中,为你没有的凭证添加资源:

    resources :credentials
    

    【讨论】:

    • 是的belongs_to user 是正确的,我修复了原始帖子。疲劳引起的错字。对此感到抱歉。
    • 正在处理...当前在渲染credentials/new.html.erb within layouts/application 时遇到错误
    • 在你的application.html.erb的body元素中,你有&lt;%= yield %&gt;吗?
    • 我不明白为什么 @cred 是 nil,因为我们在凭据中用 @cred=current_user.build_credentials 实例化了它#new。 NilClass:Class 的错误消息是ActionView::Template::Error (undefined method model_name':` 另外,在您的回答中,您使用方法 build_credential (no s) 以及在其他任何地方使用凭据 (with s)。我的控制器/模型称为凭据(带 s)
    • 我发现问题在于我的类/模型的命名。您的代码有效,谢谢。我错误地复数了我的型号名称。这篇文章让我看到了曙光:stackoverflow.com/questions/10078139/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多