【问题标题】:Find a record using form params in rails "couldn't find error"使用rails中的表单参数查找记录“找不到错误”
【发布时间】:2015-05-23 23:34:14
【问题描述】:

我是 Rails 新手。我正在尝试使用表单字段中提交的值查找“类别”记录。由于我一直使用通过 params[:id] 查找 url 参数,我认为它适用于表单参数。

这是我的错误

Couldn't find Category with 'id'=

on this line:

 @category = Category.find(params[:category_id])

这是我的代码

posts_controller.rb

def delete
  @post = Post.find(params[:id])
  @category = Category.find(@post.category_id)
  @post_archive = PostArchive.new
end

def destroy
 @post = Post.find(params[:id])
 *@category = Category.find(params[:category_id])* <--the error hits here
 @old_id = params[:post_id]
 @author_id = params[:author_id]
 @followers = Follower.find(post_id: @old_id)
 @post_archive = PostArchive.new

 PostArchive.create!(post_id: @old_id, author_id:   @author_id , removed_by: current_user.id,
  category_id: @category.id, 
  post_created_at: @post.created_at )
 @post.destroy

 @followers.each do |follower|
       ProjectMailer.post_deletion(current_user, @category, @author_id, follower, @old_id ).deliver
  end
  @followers.destroy_all
  redirect_to posts_path, notice: 'Project Deleted' 
end

表格

<%= form_for :delete_post, url: post_destroy_path(@post), method: :delete do |f| %>
<%= f.hidden_field :author_id@post.author_id %>
<%= f.hidden_field :category_id, @post.category_id  %>
<%= f.hidden_field :post_id, value: @post.id %>
Are you sure you want to delete <%=@post.title %>?
<%=f.submit %>
<% end %>


我已经测试过我可以使用 Categorgy.find(2) 找到参数并且我已经测试过参数实际上显示在表单中(它是一个隐藏字段....所以我需要)


服务器日志:

Processing by PostsController#destroy as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"s2pglEj+LUIvZ8OJ0i/sbb3T8hDTcFrqdV0rJqa3c/pihtrMez4S5A8bK3NmoQ/BleKrMRuMTUhZvCwl+00jeQ==", "delete_post"=>{"author_id"=>"21", "category_id"=>"2", "post_id"=>"417"}, "commit"=>"Delete Post", "id"=>"417"}

【问题讨论】:

  • 试试这个@category = Category.find(params[:delete_post][:category_id])

标签: ruby-on-rails forms ruby-on-rails-4 destroy


【解决方案1】:

从参数中可以看出,category_iddelete_post 内部

Parameters: {"delete_post"=>{"author_id"=>"21", "category_id"=>"2", "post_id"=>"417"}, "commit"=>"Delete Post", "id"=>"417"}

应该是

@category = Category.find(params[:delete_post][:category_id])

【讨论】:

  • 谢谢...我看到你在正式答案之前在cmets中发布了答案。
【解决方案2】:
def destroy
 @post = Post.find(params[:id])
 *@category = Category.find(params[:category_id])* <--the error hits here
 # change to 
 # *@category = Category.find(params[:delete_post][:category_id])*     
 @old_id = params[:post_id]
 @author_id = params[:author_id]
 @followers = Follower.find(post_id: @old_id)
 @post_archive = PostArchive.new

 PostArchive.create!(post_id: @old_id, author_id:   @author_id , removed_by: current_user.id,
  category_id: @category.id, 
  post_created_at: @post.created_at )
 @post.destroy

 @followers.each do |follower|
       ProjectMailer.post_deletion(current_user, @category, @author_id, follower, @old_id ).deliver
  end
  @followers.destroy_all
  redirect_to posts_path, notice: 'Project Deleted' 
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    相关资源
    最近更新 更多