我是否正确处理了这份 Rails 表格?

发布于 2024-12-12 16:53:54 字数 1416 浏览 1 评论 0原文

在我的关联控制器中,我有以下内容:

class RelationshipsController < ApplicationController

  def new
    @user_id = User.find_by_id(params[:user_id])
    @relationship = Relationship.new
  end

  def create
    @relationship = Relationship.new(params[:relationship])
    @relationship.rel_id = User.find_by_id(params[:user_id])
    @relationship.user_id = current_user
    if @relationship.save
        redirect_to root_url, :notice => "Signed Up!"
    else
        render "new"
    end
  end
end

在我的观点中,我有:

<section id="main">
  <%= form_for [@user_id, @relationship]  do |f| %>
    <div class="field">
      <%= f.label :type %>
      <%= select_tag(:type, options_for_select([['Friend', 0], ['Family', 1],['Spouse', 2]])) %>
    </div>
   <div class="actions"><%= f.submit %></div>
  <% end %>
</section>

我有几个问题:

  1. 这是处理 rel_id 和 user_id 的正确方法吗?对我来说这似乎有点笨拙。

  2. 我无法将 :type 保存到数据库,但其他一切都可以。我在服务器日志中发现以下内容:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"z7R4tWSSVHZmFXfh8HocfyuegZ2rwuXXeTLKbR+cLfs=", "type"=>"0", "commit "=>"创建关系", "user_id"=>"7"}

这对我来说似乎很奇怪,因为它应该是保存类型。

3.. 如果我在 <%= form_for [@user_id, @relationship] do |f| 中使用 @user_id 或 @current user 有关系吗? %> 行?为什么?

In my relationships_controller I have the following:

class RelationshipsController < ApplicationController

  def new
    @user_id = User.find_by_id(params[:user_id])
    @relationship = Relationship.new
  end

  def create
    @relationship = Relationship.new(params[:relationship])
    @relationship.rel_id = User.find_by_id(params[:user_id])
    @relationship.user_id = current_user
    if @relationship.save
        redirect_to root_url, :notice => "Signed Up!"
    else
        render "new"
    end
  end
end

and in my views I have:

<section id="main">
  <%= form_for [@user_id, @relationship]  do |f| %>
    <div class="field">
      <%= f.label :type %>
      <%= select_tag(:type, options_for_select([['Friend', 0], ['Family', 1],['Spouse', 2]])) %>
    </div>
   <div class="actions"><%= f.submit %></div>
  <% end %>
</section>

I have a few questions:

  1. Is this the correct way to handle the rel_id and the user_id? It seems kinda clunky to me.

  2. I can't get the :type to save to the database, but everything else does. I find the following in my server logs:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"z7R4tWSSVHZmFXfh8HocfyuegZ2rwuXXeTLKbR+cLfs=", "type"=>"0", "commit"=>"Create Relationship", "user_id"=>"7"}

which seems odd to me because it should be saving type.

3.. Does it matter if I use @user_id or @current user in the <%= form_for [@user_id, @relationship] do |f| %> line? Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳哥哥 2024-12-19 16:53:58

在 Rails 中调用属性“type”是禁忌,因为它已经在继承自 ActiveRecord::Base 的类上定义了类型方法。它用于单表继承。

最不痛苦的事情是将该列重命名为“relationship_type”或“kind”,但如果您确实需要,您可以像这样绕过它:

@relationship = Relationship.new(params[:relationship])
@relationship[:type] = params[:type]

Calling an attribute "type" is a no-no in rails, because it already defines a type method on your classes that inherit from ActiveRecord::Base. It is used for single-table-inheritance.

The least painful thing would be to rename that column to "relationship_type" or "kind", but you can get around it like this if you really need to:

@relationship = Relationship.new(params[:relationship])
@relationship[:type] = params[:type]
站稳脚跟 2024-12-19 16:53:57

1) @user_id 实际上分配了一个 User 的实例,所以我将其称为 @user
当分配rel_id时,我认为你只需要用户id值(整数)。您可能可以这样做:

@relationship.rel_id = params[:user_id]

2) type 字段用于 ActiveRecord 的 STI 表,如果您出于任何其他原因将字段命名为 type ,则会发生不好的事情。尝试将其更改为其他名称,例如 relationship_type

3) 可能,具体取决于您的应用的设置方式。 @user(_id) 和 @current_user 可能代表不同的用户。如果您只想当前用户为自己创建关系,那么您可以使用@current_user(在这种情况下可能不使用嵌套路由)。

1) @user_id is actually assigned an instance of User, so I would call it @user
when assigning rel_id I think you just need the user id value (integer). You can probably just do this:

@relationship.rel_id = params[:user_id]

2) type fields are used for STI tables with ActiveRecord, and bad things happen if you name your field type for any other reason. Try changing it to another name like relationship_type

3) Possible, depending on how your app is set up. It's possible that @user(_id) and @current_user represent different users. If you only want the current user to create a relationship for himself/herself, then you can just use @current_user (and maybe not use nested routes in that case).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文