【问题标题】:undefined method `errors' for nil:NilClass in railsnil:NilClass 在rails中的未定义方法“错误”
【发布时间】:2020-01-15 10:57:07
【问题描述】:

我已经创建了项目脚手架和舞台脚手架。我在项目的显示页面上呈现了一个按钮,以打开一种舞台形式。但我没有得到任何方法错误。我想将 project_id 传递给创建的阶段。我在项目和阶段之间有一对多的关联。 error

projects_controller.rb

  def index
    @projects = current_user.projects.all.paginate(page: params[:page], per_page: 15)
  end

  def show
    @project=Project.find(params[:id])
    @stages = Stage.all
  end

stages_controller.rb

  def index
    @stages = Stage.all
  end

  def show
  end

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


  def create
    @project = Project.find(params[:project_id])
    @stage = @project.stages.build(stages_params)

    respond_to do |format|
      if @stage.save
        format.html { redirect_to @stage, notice: 'Stage was successfully created.' }
        format.json { render :show, status: :created, location: @stage }
      else
        format.html { render :new }
        format.json { render json: @stage.errors, status: :unprocessable_entity }
      end
    end

routes.rb

  resources :projects do
    resources :stages
  end

模型项目.rb

has_many :stages

搜索路线

             Prefix Verb   URI Pattern                                                                              Controller#Action
                    tasks GET    /tasks(.:format)                                                                         tasks#index
                          POST   /tasks(.:format)                                                                         tasks#create
                 new_task GET    /tasks/new(.:format)                                                                     tasks#new
                edit_task GET    /tasks/:id/edit(.:format)                                                                tasks#edit
                     task GET    /tasks/:id(.:format)                                                                     tasks#show
                          PATCH  /tasks/:id(.:format)                                                                     tasks#update
                          PUT    /tasks/:id(.:format)                                                                     tasks#update
                          DELETE /tasks/:id(.:format)                                                                     tasks#destroy
           project_stages GET    /projects/:project_id/stages(.:format)                                                   stages#index
                          POST   /projects/:project_id/stages(.:format)                                                   stages#create
        new_project_stage GET    /projects/:project_id/stages/new(.:format)                                               stages#new
       edit_project_stage GET    /projects/:project_id/stages/:id/edit(.:format)                                          stages#edit
            project_stage GET    /projects/:project_id/stages/:id(.:format)                                               stages#show
                          PATCH  /projects/:project_id/stages/:id(.:format)                                               stages#update
                          PUT    /projects/:project_id/stages/:id(.:format)                                               stages#update
                          DELETE /projects/:project_id/stages/:id(.:format)                                               stages#destroy
                 projects GET    /projects(.:format)                                                                      projects#index
                          POST   /projects(.:format)                                                                      projects#create
              new_project GET    /projects/new(.:format)                                                                  projects#new
             edit_project GET    /projects/:id/edit(.:format)                                                             projects#edit
                  project GET    /projects/:id(.:format)                                                                  projects#show
                          PATCH  /projects/:id(.:format)                                                                  projects#update
                          PUT    /projects/:id(.:format)                                                                  projects#update
                          DELETE /projects/:id(.:format)                                                                  projects#destroy

项目 show.html.erb

<%= link_to "Add Stage", new_project_stage_url(@project), :class=>"button primary small" %>

updated error

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-5


    【解决方案1】:

    您正在使用嵌套路由,但在 form_with 中您没有指定。尝试像这样更改form_with:

    form_with(model: stage, url: [@project, stage])
    

    来源:https://stackoverflow.com/a/46919792/4207394

    您必须检查变量名称,即stage 变量应具有新的Stage 对象,@project 变量应具有Project 对象,否则请相应地更改名称。

    【讨论】:

    • 嘿,现在我可以呈现表单,但在提交时我无法将数据保存到数据库中的阶段表中。
    • 您可以检查日志(运行命令 rails s 的终端)以查看您在提交表单时遇到的错误。
    【解决方案2】:

    我认为你有一个错误,因为在你的新方法中你设置了一个名为 @stages 的变量

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

    但在您看来,您正在使用一个名为@stage 的变量。

    只需在您的控制器方法#new 中将@stages 更改为@stage,您就可以这样做了:

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

    在你看来:

    <% if @stage.errors.any? %>
    

    【讨论】:

    • def new @stage = Stage.new @project = Project.find(params[:project_id]) end 为 #:0x00007f39800dbb30> 生成错误 - 未定义方法 `stages_path'
    • 编辑:当您在控制台中输入rails routes 时,您会看到stages_path 路线吗?我认为您正在尝试使用您的应用中不存在的路线
    • 我已经添加了有问题的路线
    猜你喜欢
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多