在隐藏字段中传递帖子 ID 的值 [英] Passing Value of Post ID in Hidden Field

查看:32
本文介绍了在隐藏字段中传递帖子 ID 的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我的另一个问题一致;当我提交属于特定帖子的新评论时,尝试获取正确的 post_id.

This goes along with my other question; trying to get the correct post_id to go along when I submit a new Comment that belongs that a specific Post.

_form.html.erb

_form.html.erb

<%= f.hidden_field :project_id, :value => params[:id] %>
<%= f.hidden_field :post_id, :value => params[:id].post_id %>
<%= f.hidden_field :user_id, :value => current_user.id %>

推荐答案

在没有看到更多代码的情况下很难确切地知道您在做什么,但我的猜测是使用 @post 设置了一个 @post 实例变量= Post.find(params[:id]) 在控制器中

kind of hard to know exactly what you are doing without seeing more code, but my guess is that a @post instance variable has set using @post = Post.find(params[:id]) in the controller

# this is not needed, on the create, get it from the post?
<%= f.hidden_field :project_id, :value => params[:id] %>
# if you do want to pass it, guessing something like this
<%= f.hidden_field :project_id, :value => @post.project_id %>

# pass the post id to the create action
<%= f.hidden_field :post_id, :value => @post.id %>
# if the comment has a project_id
# @comment.project = @post.project ?

# do not send this in hidden field, get the value in your controller
# otherwise the user can change this value to another user when submitting the form
<%= f.hidden_field :user_id, :value => current_user.id %>

您可能还想考虑在注释的情况下使用嵌套路由?

you might also want to consider using nested routes in the case of comments?

我建议从 rails 指南中阅读有关视图助手的信息:http://guides.rubyonrails.org/index.html

I recommend reading about view helpers from the rails guides: http://guides.rubyonrails.org/index.html

http://guides.rubyonrails.org/getting_started.html 页面实际上有代码带有评论的帖子示例

the http://guides.rubyonrails.org/getting_started.html page actually has code examples for a post with comments

这篇关于在隐藏字段中传递帖子 ID 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆