Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActiveRecord无法';我找不到与';id'=_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails ActiveRecord无法';我找不到与';id'=

Ruby on rails ActiveRecord无法';我找不到与';id'=,ruby-on-rails,ruby,Ruby On Rails,Ruby,我在stackoverflow上搜索解决方案,但每次都有不同的问题,所以我决定问这个问题。 在我的申请表上,我有很多职位。一个用户可以创建多个旅行,在一次旅行中发布多个帖子。 但是,当我尝试创建帖子时,出现以下错误: ActiveRecord::RecordNotFound in PostsController#create Couldn't find Travel with 'id'= 我不明白为什么如果有人能帮我。。 以下是我的帖子\u controller.rb(创建操作): 以下是我对

我在stackoverflow上搜索解决方案,但每次都有不同的问题,所以我决定问这个问题。 在我的申请表上,我有很多职位。一个用户可以创建多个旅行,在一次旅行中发布多个帖子。 但是,当我尝试创建帖子时,出现以下错误:

ActiveRecord::RecordNotFound in PostsController#create
Couldn't find Travel with 'id'=
我不明白为什么如果有人能帮我。。 以下是我的帖子\u controller.rb(创建操作):

以下是我对模型的看法:

class Post < ActiveRecord::Base
  belongs_to :user
  belongs_to :travel

  geocoded_by :country
  after_validation :geocode
end

class Travel < ApplicationRecord
  has_many :posts
  belongs_to :user
end
和一个用于形式的ligne:

<%= form_for(@post, :html => {class: "form-horizontal", role: "form"}, :url => travel_posts_path(@travel)) do |f| %>
        <div class="form-group">
            <%= f.text_field :travel_id, placeholder: @travel.id %>
        </div>
{class:“表单水平”,role:“表单”},:url=>travel_posts_path(@travel))do | f |%>

参数[:id]
将为您提供
贴子的
id
,您可以使用它查找
旅行
。这可能不会给你带来潜在的错误和错误的旅行,但这不是你想要的。您在表单中指定了
travel\u id
,因此要查找的是
params[:travel\u id]


将来,您可能希望使用控制器函数顶部右侧的
调试器
,并
放置
参数
对象以查看其中的内容。这将帮助您了解是否调用了错误的
键,以及当事情变得复杂时结构是什么样子。

为什么允许用户处理
id
s?这是一种糟糕的做法。我建议调试
params
。您可能需要执行类似于
params[:travel\u id]
的操作。但是我仍然不允许用户选择
id
s.@Dbz我只是尝试在我的表post的:travel\u id列中添加旅行id,表单中的字段只是为了帮助我,我会在它起作用时删除该行。我猜
id
将返回
post\u id
,而不是
travel\u id
,因此,当您使用它搜索旅行时,它找不到它,因为它比所有旅行的数字都大。@Dbz您是对的,当我将:travel\u id替换为:id时,它工作了!非常感谢你的帮助!
# Travel
    resources :travels, :shallow => true do
        # Posts
        resources :posts
    end
<%= form_for(@post, :html => {class: "form-horizontal", role: "form"}, :url => travel_posts_path(@travel)) do |f| %>
        <div class="form-group">
            <%= f.text_field :travel_id, placeholder: @travel.id %>
        </div>