【问题标题】:best_in_place & model attribute accessing (noob?)best_in_place 和模型属性访问(菜鸟?)
【发布时间】:2012-05-01 18:48:53
【问题描述】:

我才学习 Rails 大约一个月,所以我不确定这里的答案是真的简单还是更复杂。

我有两个模型,我们称它们为“SpecialUser”和“User”。

我的用户模型如下所示:

class User < ActiveRecord::Base
has_one :special_user
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :roles, :user_type, :address_1, :address_2, :city, :state, :zip

(&c.)

我的 SpecialUser 模型看起来像这样:

class SpecialUser < ActiveRecord::Base
belongs_to :user

(&c.)

我们正在使用https://github.com/bernat/best_in_place 创建一个表单来编辑 SpecialUser 的信息。以前,我们为 SpecialUser 提供了一个单独的“地址”属性,但最近取消了它以支持指定完整地址(地址_1、地址_2、城市、州、邮编...)并将其放入用户中,以便普通用户可以也有地址。 :)

但是,当我尝试这样做时:

<% # best_in_place_if can?(:edit, @super_user), @super_user.user, :address_1 %>

我收到此错误:

NoMethodError in Project_professionals#show

undefined method `user_path' for #<#<Class:0x0000013457fb90>:0x00000132e1fdd8>

我也尝试将@super_user.user 替换为User.find(@super_user.user)User.find(@super_user.user_id),但我得到了完全相同的错误。

当我这样做时

<%= @super_user.user.address_1 %>

有效!

阅读完 best_in_place 的资料后,我仍然无法找到答案。 :( 不确定这是我们使用的 gem 的限制还是我遗漏了什么。

【问题讨论】:

    标签: nested-attributes ruby-on-rails-3.2 best-in-place


    【解决方案1】:

    best_in_place 应该处理它——没有限制

    看看能不能解决

    :path: 更新操作将被发送到的 URL。如果没有定义 它默认为 :object 路径。

    <%= best_in_place_if can?(:edit, @super_user), @super_user.user, :address_1, :path => super_user_user_path %>
    

    【讨论】:

    • 太棒了!非常感谢! :) 实际上我只需要使用super_user_path 而不是super_user_user_path,因为后者不存在。胡闹,发现常规路径工作得很好。 :) 编辑:所以,我说得太早了....它将显示来自数据库的数据,当我单击编辑它时,控制台日志会抛出此错误:Uncaught SyntaxError: Unexpected token &lt;。我会继续胡闹,但你能想到什么能让这件事消失吗? :)
    • 最终放弃了这一点,将地址数据移入 super_user 表并移出 user 表。
    猜你喜欢
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    相关资源
    最近更新 更多