Ruby on rails 将用户id和投票id传递给注释
我是一名新的rails开发人员,目前正在从事一个项目,用户可以将参数发布到投票中。(或对帖子的评论) 我有一个设计用户模型,一个投票模型和一个参数模型 我的问题是,我似乎无法访问用户属性,因此我可以将Ruby on rails 将用户id和投票id传递给注释,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我是一名新的rails开发人员,目前正在从事一个项目,用户可以将参数发布到投票中。(或对帖子的评论) 我有一个设计用户模型,一个投票模型和一个参数模型 我的问题是,我似乎无法访问用户属性,因此我可以将添加到视图中的参数中。 我的问题是,argument.user.first\u name返回以下错误:undefined methodfirst\u name'表示nil:NilClass` 当我尝试使用时,效果非常好 当我去控制台找到投票和参数并执行argument.user.first\u na
添加到视图中的参数中。我的问题是,argument.user.first\u name返回以下错误:
undefined method
first\u name'表示nil:NilClass`
当我尝试使用
时,效果非常好
当我去控制台找到投票和参数并执行argument.user.first\u name
时,我得到了正确的名字
迁移过程如下所示:
create_table "arguments", force: true do |t|
t.string "title"
t.text "argument"
t.integer "vote_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
class ArgumentsController < ApplicationController
before_filter :current_user
def create
@vote = Vote.find(params[:vote_id])
@argument = @vote.arguments.build(params[:argument].permit(:argument))
@argument.user_id = current_user.id
@argument.save!
if @argument.save
redirect_to @vote
else
render 'new'
end
end
private
def argument_params
params.require(:argument).permit(:title, :argument)
end
end
所以我在arguments表中有来自用户和投票的外键
我在投票和用户上都添加了“has_many”关系。因此,用户和投票都有很多争论
我的参数控制器如下所示:
create_table "arguments", force: true do |t|
t.string "title"
t.text "argument"
t.integer "vote_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
class ArgumentsController < ApplicationController
before_filter :current_user
def create
@vote = Vote.find(params[:vote_id])
@argument = @vote.arguments.build(params[:argument].permit(:argument))
@argument.user_id = current_user.id
@argument.save!
if @argument.save
redirect_to @vote
else
render 'new'
end
end
private
def argument_params
params.require(:argument).permit(:title, :argument)
end
end
希望你们中的一个能帮助我,指引我走向正确的方向!
提前感谢:)由于您的
参数
模型已属于:user
您可以指定@argument.user=current\u user
而不是id。由于当前用户
已加载,因此开销最小,而且参数
无需重新加载即可使用
与问题无关,但需要修复:
save
/save
很奇怪,通常您只需要一个,尤其是在稍后的代码中检查保存是否成功时(现在您将出现验证错误异常,而不是表单渲染)argument\u params
调用被替换为params[:argument]。允许(:argument)
(如果遇到没有参数的请求,将产生关于nil
的错误,而不是必需的参数
)user\u id
隐藏字段无论如何都会被强参数丢弃,最好将其删除(并且不允许可能的冒名顶替者)您需要在控制器中对用户进行身份验证,如:-
before_filter :authenticate_user!
@argument.user_id = current_user.id
更改参数控制器:-
class ArgumentsController < ApplicationController
before_filter :authenticate_user!
def create
@vote = Vote.find(params[:vote_id])
@argument = @vote.arguments.build(argument_params)
if @argument.save
redirect_to @vote
else
render 'new'
end
end
private
def argument_params
params.require(:argument).permit(:title, :argument, :user_id)
end
end
啊,终于!我设法解决了自己的愚蠢问题 问题是我在参数之前呈现表单(创建参数)。这创建了一个不包含任何内容的空参数div/object。因此,错误总是出现在空对象上(obv不包含用户信息,因为它是空的) 所以你的答案是正确的,但我的问题是错误的,哈哈 好的,谢谢你的时间 在我的show.html erb中,我添加了
<%= render 'arguments/argument' %>
<div class="jumbotron">
<%= render 'arguments/form' %>
</div>
谢谢您的回答,请注意:)当使用@argument.user=current\u user时,我仍然会遇到错误。。。在我看来,在创建参数时,似乎没有正确地传递用户参数?是的。当我添加到我的视图中时,我得到了正确的用户id。最好不要从表单参数传递user\u id
,这样用户就可以在表单中传递其他用户的id,这是有意义的。我宁愿从控制器获取用户,也不愿通过隐藏的字段获取。。我只是在它无法工作时尝试添加它。但是,当我试图显示argument.user.first_name(或除id之外的任何其他名称)时,仍然会出现错误,这可能是因为我试图从另一个视图访问designe usermodels数据吗?我一直在研究和助手方法,您可以从任何视图访问Desive用户模型。您需要确保argument.user不是nil,即在argument中user_id不是nil。
@argument.user_id = current_user.id
<%= render 'arguments/argument' %>
<div class="jumbotron">
<%= render 'arguments/form' %>
</div>