如何将多个值传递给 Rails 中的创建操作?

发布于 2024-12-07 23:02:29 字数 1038 浏览 0 评论 0原文

在我的应用程序中,我有 UserPostComment 模型。

User想要对Post发表评论时,Comments控制器中的新操作将接管。显示帖子(待评论),并且用户输入他的评论

但是,当用户提交时,我想将 Post.id 和 Comments.content 传递给创建操作。我该怎么做?

这是comments/new.html.erb

<%= form_for @comment do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.text_area :comment %>
</div>
<div class="actions">
<%= f.submit "Done" %>
</div>
<% end %>

谢谢大家。我做了嵌套路由,我的 new.html.erb 现在有

<%= form_for [@post,@comment] do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<% f.hidden_field :post %>
<div class="field">
<%= f.text_area :comment %>
</div>
<div class="actions">
<%= f.submit "Done" %>
</div>
<% end %> 

但是我得到:未定义的方法“注释”,我无法弄清楚那个错误。

In my app I have User, Post and Comment models.

When a User wants to comment on Post the new action from the Comments controller takes over. The Post (to be commented on) is shown and the User enters his Comment.

However, when the User submits, I want to pass the Post.id and the Comments.content to the create action. How do I do that?

Here is the comments/new.html.erb

<%= form_for @comment do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.text_area :comment %>
</div>
<div class="actions">
<%= f.submit "Done" %>
</div>
<% end %>

Thanks to all of you. I did the nested routing and my new.html.erb now has

<%= form_for [@post,@comment] do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<% f.hidden_field :post %>
<div class="field">
<%= f.text_area :comment %>
</div>
<div class="actions">
<%= f.submit "Done" %>
</div>
<% end %> 

However I get: undefined method `comment' and I cant figure that bugger out.

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

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

发布评论

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

评论(3

夏尔 2024-12-14 23:02:29

我的猜测是,每个 Comment 必须属于一个 Post 如果是这种情况,那么这似乎是嵌套路由的完美候选者。 http://guides.rubyonrails.org/routing.html#nested-resources

resources :posts do
  resources :comments
end

因此,在您的情况下,帖子 id 和评论 id 都将成为 URL 的一部分:

# Will submit to a URL like /posts/1/comments
# or /posts/1/comments/1
<%= form_for [@post,@comment] do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <div class="field">
    <%= f.text_area :comment %>
  </div>
  <div class="actions">
    <%= f.submit "Done" %>
  </div>
<% end %>

您需要在评论控制器操作中处理 post_id 。

My guess is that each Comment must belong to a Post If that's the case then this seems like the perfect candidate for nested routes. http://guides.rubyonrails.org/routing.html#nested-resources

resources :posts do
  resources :comments
end

So in your case both the post id and the comment id would be part of the URL:

# Will submit to a URL like /posts/1/comments
# or /posts/1/comments/1
<%= form_for [@post,@comment] do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <div class="field">
    <%= f.text_area :comment %>
  </div>
  <div class="actions">
    <%= f.submit "Done" %>
  </div>
<% end %>

You would need to handle the post_id in your comments controller actions.

挽梦忆笙歌 2024-12-14 23:02:29

首先,您必须将 Post.id 传递给评论新操作。就像

link_to "Add comment", new_comment_path( params[ :id ] )

我假设您遵循约定,因此 params[ :id ]Post.id。稍后在您的 Comment#create 中实例化新评论

@comment = Comment.new( :post_id => params[ :id ] )

,用于创建与帖子相关的评论。最后形成新评论

<%= form_for @comment do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <%= f.hidden_field :post_id %>
  <div class="field">
    <%= f.text_area :comment %>
  </div>
  <div class="actions">
    <%= f.submit "Done" %>
  </div>
<% end %>

First of all you have to pass Post.id to the comments new action. Something like

link_to "Add comment", new_comment_path( params[ :id ] )

I assume that you're following conventions so params[ :id ] is Post.id. Later in your Comment#create instantiate new comment with

@comment = Comment.new( :post_id => params[ :id ] )

which will create comment related to the post. Finally form for new comment

<%= form_for @comment do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <%= f.hidden_field :post_id %>
  <div class="field">
    <%= f.text_area :comment %>
  </div>
  <div class="actions">
    <%= f.submit "Done" %>
  </div>
<% end %>
你与清晨阳光 2024-12-14 23:02:29

在视图(使用 HAML)

=form_for( @comment, :as => :comment) do |f|
  =f.hidden_field :post_id
  =f.hidden_field :user_id
  =f.text_area :comment
  =f.submit "Submit"

和 Comment#new 控制器中:

@comment = Comment.new(:user_id => @user.id, :post_id => @post.id)

In the view (using HAML)

=form_for( @comment, :as => :comment) do |f|
  =f.hidden_field :post_id
  =f.hidden_field :user_id
  =f.text_area :comment
  =f.submit "Submit"

and in the Comment#new controller:

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