0

我是开发新手,也是 Rails 新手。

我有 2 个资源项目和 Pod。//Pods 就像子项目

-----config/routes.rb------

  resources :projects
  resources :pods

------模型/project.rb------

    class Project < ActiveRecord::Base
      attr_accessible :description, :name

      **has_many :pods, dependent: :destroy**

      before_save { |project| project.name = name.upcase }

      validates :name, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
      validates :description, presence: true
    end

------模型/pod.rb------

    class Pod < ActiveRecord::Base
      attr_accessible :description, :name

      belongs_to :project


      before_save { |pod| pod.name = name.upcase }

     validates :name, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
      validates :description, presence: true
      validates :project_id, presence: true

      default_scope order: 'pods.created_at DESC'
    end

在项目视图中,我有一个创建 pod 的链接

<p> <%= link_to "Create new Pod", new_pod_path(project: @project.id) %></p>

在这里,我将项目 ID 发送到 new_pod_path,以便在创建 pod 时,它可以与这个特定项目相关联。

------Pod 控制器-----

def new
  @project = Project.find_by_id(params[:project])
  @pod = Pod.new
end

def create
  @pod = @project.pods.build(params[:pod])
    if @pod.save
      flash[:success] = "Congratulaions! You have created a new pod."
      redirect_to @pod
    else
      render 'new'
    end
  end

=======================

当我单击“创建新 pod”链接时,会显示表单并在调试参数中看到

project: '1'
action: new
controller: pods

我填写详细信息并单击提交,我收到以下错误

NoMethodError in PodsController#create

undefined method `pods' for nil:NilClass

我不确定这里发生了什么。我现在不想拥有嵌套资源,因为 pod 将拥有测试套件,然后测试套件将拥有测试用例。

任何帮助将不胜感激

====================================== 编辑----

我不知道出了什么问题。让我说得更清楚。我有一个项目展示页面,其中包含带有项目 ID 的 new_pod_path ----"

<%= link_to "创建新 Pod", new_pod_path(project_id: @project.id) %>

" ---------- 这将在 pod 控制器中执行新操作.. 我的代码为 -----

  def new
    @project = Project.find(params[:project_id])
    @pod = Pod.new
  end

我是否需要在新的行动中找到项目?

new 的 pod 视图:

    <%= f.hidden_field  :project_id, value: @project.id %> 

//我从新方法中猜测,视图将能够看到@project.id,并将上述代码包含在新操作中以查找项目是有意义的 //如果我错了请纠正我------- -

然后我进行创建--------

   def create
        @project = Project.find_by_id(params[:project_id])
        @pod = @project.pods.build(name: params[:name], description: params[:description])
        if @pod.save ...

我不知道发生了什么事。例如,如果您能告诉我流程——项目显示页面——调用 new_pod_path 并在参数中使用项目 ID。新的 pod 操作采用该 id 并找到项目或以某种方式使其以新的 pod 形式可用。新的 pod 表单使用 taht id 并作为 hidden_​​field 传递.. 然后创建操作通过参数获取隐藏字段并找到项目并创建 project.pods.build(... 我真的很感激

------------- 服务器日志 ------------- 似乎只有项目 id 有问题 ---------- Started POST "/pods " for 127.0.0.1 at 2013-03-15 17:55:56 -0700 由 PodsController#create 作为 HTML 参数处理:{"utf8"=>"✓", "authenticity_token"=>"fXCOH0kUgHHVtrH79d4VCrHWJakdGQ8cKQmiJlE0OV4=", "pod" =>{"project_id"=>"1", "name"=>"sdf", "description"=>"df"}, "commit"=>"Create new pod"} 用户负载 (0.4ms) SELECT " users".* FROM "users" WHERE "users"."remember_token" = '70InmWq61iFCnjzuxhBP6g' LIMIT 1 Project Load (0.2ms) SELECT "projects".* FROM "projects" WHERE "projects"."id"IS NULL LIMIT 1 Completed 500 Internal Server Error in 4ms

NoMethodError(未定义的方法pods' for nil:NilClass): app/controllers/pods_controller.rb:23:in创建'

4

3 回答 3

1

in create you are missing

@project = Project.find(params[:project])

Make sure the project_id is in params key :project

On requests the view variables @ need to be recreated.

Edit:

How are you putting the project_id in the params hash it looks wrong (project_id"=>{"1"=>""},) (to get that id you do params[:project_id].first.key, but that's ugly)

If you do like codeit suggested,

<%= hidden_field 'project', @project.id %> <!--@project initialized in new -->

Or you can do

<%= hidden_field 'project', params[:project] %> <!--params[:project] coming from the new request -->

Then params[:project] will give you the id 1

于 2013-03-15T17:49:45.900 回答
0

您的实例变量@pod为零,这意味着您的方法将引发异常。你必须先完成你@pod的某事,然后才能使用它的方法

于 2013-03-15T20:14:18.770 回答
0

使用hidden_field_tag形式new pod

<%= hidden_field_tag 'project_id', @project.id %>   #Add this line in new pod form

控制器变化:

def create
  @project = Project.find(params[:project_id])
  @pod = @project.pods.build(params[:pod])
  if @pod.save
    flash[:success] = "Congratulaions! You have created a new pod."
    redirect_to @pod
  else
    render 'new'
  end
end
于 2013-03-15T18:02:37.323 回答