English 中文(简体)
“找不到没有ID的<object>”
原标题:"Couldn t find <object> without an ID"

我在实现一种评论表单时遇到了问题,在这种表单中,评论(称为“微帖子”)属于用户和帖子,用户有很多评论,帖子(也称为“命题”)有很多评论。

我的评论表单代码是:

<%= form_for @micropost do |f| %>
  <div class="field">
      <%= f.text_area :content %>
  </div>

  <div class="actions">
    <%= f.submit "Submit" %>
  </div>
<% end %>

MicropostsController在创建操作中包含以下内容:

  def create

      @proposition = Proposition.find(params[:proposition_id])

  @micropost = current_user.microposts.build(params[:micropost])
  @micropost.proposition = @proposition
  if @micropost.save
      flash[:success] = "Contribution submitted"
      redirect_to root_path
  else
          @feed_items = []    
      render  pages/home 
  end
  end

创建新微柱的表格与命题在同一页上,但命题id似乎在任何时候都没有通过。

这是我在提交微邮报表格时遇到的错误:

ActiveRecord::RecordNotFound in MicropostsController#create

Couldn t find Proposition without an ID

参数为:

{"commit"=>"Submit", "micropost"=>{"proposition_id"=>"", "content"=>"First comment"}, "authenticity_token"=>"TD6kZaHv3CPWM7xLzibEbaLJHI0Uw43H+pq88HLZFjc=", "utf8"=>"✓"}

我对rails完全陌生,对任何编码都非常陌生,所以我非常感谢你能给我的任何帮助!

提前谢谢。

最佳回答

您的参数是:

"micropost"=>{"proposition_id"=>"", "content"=>"First comment"}

因此,要获得proposition_id,您必须执行以下操作:

params[:micropost][:proposition_id]

但这是空的。没有其他地方可以获得这个<code>id</code>,这就是为什么这一行检索<code>nil</code〕:

@proposition = Proposition.find(params[:proposition_id])

使其失败:

@micropost.proposition = @proposition

您必须:

  • 添加proposition_id作为隐藏字段

  • 将其存储在会话中

但我对你的背景了解不足以给你一个合适的解决方案。

编辑:

在链接中,替换:

<%= f.hidden_field :proposition_id %>

与:

<%= f.hidden_field :proposition_id, :value => @proposition.id %>

如果它不起作用,显示你的参数。

注意:依赖实例变量是不好的做法,您应该将局部变量发送到每个分部

问题回答

如您所见,proposition_id参数为空,因此除非您给它一个有效的id,否则您的控制器无法找到该命题。

您需要确保表单发送proposition_id属性。一种方法是:

  1. 在控制器中的操作中设置命题:@micpost.propoosition=

  2. 在表单中,为id添加一个隐藏字段:f.hidden_field:proposition_id

  3. create操作中,使用params[:micropost][:Proposition_id]找到合适的命题

(您还需要确保在Micropost模型中使用attr_accessible,并确保proposition_id不在该列表中。否则,您将面临严重的安全漏洞。请参阅http://www.kalzumeus.com/2010/09/22/security-lessons-learned-from-the-diaspora-launch/应保护哪些字段不受批量分配的影响?

编辑(由于评论):

你的新操作应该是这样的:

def new
  @micropost = Micropost.new
  @micropost.proposition_id = params[:proposition_id]

这与上面所说的略有不同,这是因为您正在向new发送请求中的命题id。不需要查找实际的命题记录,因为我们只对id字段感兴趣(我们已经有了)。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...