简体   繁体   English

使用Rails 4找不到没有ID的帖子

[英]Couldn't find Post without an ID using rails 4

guys i am suffering from a problem from last two days and i dont know whats going wrong with my code. 伙计们,我最近两天都在遭受问题困扰,我不知道自己的代码出了什么问题。 I have two model with the name of 'post' and 'descrip'. 我有两个模型,名称分别为“ post”和“ descrip”。 Descrip belongs_to post as post has_one descrip. 将has_one描述记为descrip。 So when i submit form for post then next form appear which is for descrip.I descrip form i have one hidden field to pass post_id. 因此,当我提交要提交的表单时,会出现下一个要描述的表单。我描述表单时,我有一个隐藏字段可以传递post_id。 But when i submit descrip form an error appear that "POST can't be find without an id". 但是,当我提交说明表单时,出现错误,即“没有ID便无法找到POST”。 Here below my code as in view 这是我的代码下面的视图

<%= form_for :descrip, url:{action: "create", :controller => "descriptions"} do |f| %>
  <li>
    <%= f.label 'Detail' %><br>
    <%= f.text_field :detail %>
  </li>
  <li>
    <%= f.hidden_field :post_id , :value => @post.id %>
  </li>
  <li>
    <%= f.submit %>
  </li>
<% end %>

In Descriptions_controller 在Descriptions_controller中

def new
  @descrip = Descrip.new
end

def create
  @post = Post.find(params[:descrip][:post_id])
  @descrip = @post.descrips.build(descrip_params)
  if @descrip.save
    render @post
  else
    render 'new'
  end
end

model of post and descrip 邮编模型

 class Post < ActiveRecord::Base
   has_one :descrip, :dependent => :destroy
 end

 class Descrip < ActiveRecord::Base
   belongs_to :post
 end

resources.rb resources.rb

resources :descriptions
get "descriptions/new", :to => "descriptions#new", as: :descriptions_new
post "descriptions/create/:id", :to => "descriptions#create", as::descriptions_create

resources :posts do
  resources :descriptions
end

Kindly suggest me what i should do.How i can solve my error and how i can get post_id in my decrips table.Please. 请建议我该怎么办。如何解决我的错误以及如何在我的decrips表中获取post_id。请。

It seems like there are two possibilities. 似乎有两种可能性。

1) Firstly, it could be that your params are not what you think they are and plugging in an invalid number for Post.find() . 1)首先,可能是您的参数不是您想的那样,并为Post.find()插入了无效的数字。 Could you share what your params looks like? 您能否分享您的参数外观?

2) If that's not the case you could be calling id on a post that isn't yet saved and therefore can't be found by ActiveRecord within your database. 2)如果不是这种情况,则您可能在尚未保存的帖子中调用id,因此ActiveRecord在数据库中找不到该帖子。 If you're submitting this form along with a form to create a post, you're likely saving the description first. 如果您要同时提交此表单和用于创建帖子的表单,则可能需要先保存描述。 Make sure your post either exists in the database or is saved before the description to fix this. 确保您的帖子在数据库中存在或保存在说明之前,以解决此问题。

Out of curiosity is there any reason you've made description a separate table from post? 出于好奇,您是否有任何理由将说明与发布表分开? It seems like you could save yourself a lot of trouble just having it be a text column within the posts table. 好像将它作为posts表中的text列似乎可以为您省去很多麻烦。

in your action add logger.info : 在您的操作中添加logger.info

def create
 logger.info "---params ==> #{params.inspect}---"
 @post = Post.find(params[:descrip][:post_id])
 @descrip = @post.descrips.build(descrip_params)
 if @descrip.save
  render @post
 else
 render 'new'
end

end 结束

run the application then in log/development.rb file you can view the all params value. 运行该应用程序,然后在log/development.rb文件中可以查看所有params值。 if params or not passing try with <%= hidden_field_tag 'descrip[post_id]', @post.id %> 如果参数没有传递,请尝试使用<%= hidden_field_tag 'descrip[post_id]', @post.id %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM