【问题标题】:The :id is there but i cant access it, how is this possible?:id 在那里,但我无法访问它,这怎么可能?
【发布时间】:2011-10-17 21:16:22
【问题描述】:

在浏览器地址栏中我有http://localhost:3000/comment/index?post_id=6,我可以访问索引中的post_id,但是当尝试在创建操作中创建评论/帖子时,它说在日志中找不到没有帖子ID的帖子。什么是在这里继续吗?提前谢谢你。

评论控制器:

def index
  @post=Post.find(params[:post_id])
end

def create
  @post     = Post.find(params[:post_id])
  @comment  = @post.comments.build(params[:comment])
  @comment.save

  respond_with( @comment, :layout => !request.xhr? )          
end

comments/index查看:

<%= form_for :comment, :remote => true,
                       :url => { :controller => "comments",
                                 :action     => "create"
                               },
                       :html => { :id => 'new-comment'} do |f|
%>
  <%= f.hidden_field :post_id, :value => @post.id %>
  <%= f.text_area :body %>
  <%= f.submit "post" %>
<% end %>

在日志中:

Started POST "/comments" for 127.0.0.1 at 2011-10-17 14:06:36 -0700
  Processing by CommentsController#create as JS
  Parameters: {"utf8"=>"✓",  
    "authenticity_token"=>"cxQm2K2xwsyw0DY2XLNvkcMQI+wM96LpEENbfQqxu5c=",
    "comment"=> {"post_id"=>"6", "body"=>"This is the comment"},
    "commit"=>"post"}
  Completed 404 Not Found in 23ms

  ActiveRecord::RecordNotFound (Couldn't find Post without an ID):

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 routes ruby-on-rails-3.1


    【解决方案1】:

    如果您查看日志中的 params 哈希,您会看到:

    { "utf8"=>"✓",
      "authenticity_token"=>"cxQm2K2xwsyw0DY2XLNvkcMQI+wM96LpEENbfQqxu5c=",
      "comment"=> {
          "post_id"=>"6", # <-- there's your post_id
          "body"=>"This is the comment" },
      "commit"=>"post" }
    

    所以帖子 ID 在那里,但它在 comment 哈希内。因此,在您的创建操作中,您只需更改为:

    def create
      @post=Post.find(params[:comment][:post_id])
      @comment = @post.comments.build(params[:comment])
      @comment.save
      respond_with( @comment, :layout => !request.xhr? )          
    end
    

    但是,您应该能够稍微简化一下创建操作。

    def create
      @comment = Comment.new(params[:comment])
      @comment.save
      respond_with( @comment, :layout => !request.xhr? )
    end
    

    由于post_idcomment 参数中,当您创建评论时,评论将自动与帖子关联,而无需查找记录。如果您需要访问您视图中的帖子,可以使用@comment.post

    【讨论】:

    • 谢谢艾米丽,非常有帮助
    【解决方案2】:

    请不要在表单中将您的字段作为hidden_field 传递。一个更好的方法是这样做:

    <%= form_for :comment,
                 :remote => true,
                 :url    => post_comments_path(post)
                 :html => { :id => 'new-comment'} do |f| %>
    

    通过使用 Rails 为您提供的路由助手,这将通过以下方式清理您的表单:

    1. 不让您使用丑陋的哈希语法为您的表单生成 URL
    2. 您没有在表单中放置hidden_field 以通过URL 发送;和
    3. God DHH 的意图一样,通过 post 参数自动发送 params[:post_id]

    这意味着您可以使用这行代码在您的操作中找到它:

    Post.find(params[:post_id])
    

    而不是这样,它不必要地更长,因此打字更痛苦:

    Post.find(params[:comment][:post_id])
    

    【讨论】:

      猜你喜欢
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      • 1970-01-01
      相关资源
      最近更新 更多