我是否正确处理了这份 Rails 表格?
在我的关联控制器中,我有以下内容:
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>
我有几个问题:
这是处理 rel_id 和 user_id 的正确方法吗?对我来说这似乎有点笨拙。
我无法将 :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:
Is this the correct way to handle the rel_id and the user_id? It seems kinda clunky to me.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Rails 中调用属性“type”是禁忌,因为它已经在继承自 ActiveRecord::Base 的类上定义了类型方法。它用于单表继承。
最不痛苦的事情是将该列重命名为“relationship_type”或“kind”,但如果您确实需要,您可以像这样绕过它:
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:
1)
@user_id
实际上分配了一个User
的实例,所以我将其称为@user
当分配
rel_id
时,我认为你只需要用户id值(整数)。您可能可以这样做:2)
type
字段用于 ActiveRecord 的 STI 表,如果您出于任何其他原因将字段命名为type
,则会发生不好的事情。尝试将其更改为其他名称,例如relationship_type
3) 可能,具体取决于您的应用的设置方式。 @user(_id) 和 @current_user 可能代表不同的用户。如果您只想当前用户为自己创建关系,那么您可以使用@current_user(在这种情况下可能不使用嵌套路由)。
1)
@user_id
is actually assigned an instance ofUser
, 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:2)
type
fields are used for STI tables with ActiveRecord, and bad things happen if you name your fieldtype
for any other reason. Try changing it to another name likerelationship_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).