Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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 4 未定义的方法[模型]\u嵌套资源的url_Ruby On Rails 4_Ruby 1.9.3 - Fatal编程技术网

Ruby on rails 4 未定义的方法[模型]\u嵌套资源的url

Ruby on rails 4 未定义的方法[模型]\u嵌套资源的url,ruby-on-rails-4,ruby-1.9.3,Ruby On Rails 4,Ruby 1.9.3,我正在处理嵌套资源项目,并且我的步骤控制器中出现错误: def create @step = Step.new(step_params) respond_to do |f| if @step.save f.html { redirect_to @step, notice: 'Step was successfully created.' } f.json { render action: 'show', status: :crea

我正在处理嵌套资源项目,并且我的步骤控制器中出现错误:

  def create
    @step = Step.new(step_params)

    respond_to do |f|
      if @step.save
        f.html { redirect_to @step, notice: 'Step was successfully created.' }
        f.json { render action: 'show', status: :created, location: @step }
      else
        f.html { render action: 'new' }
        f.json { render json: @step.errors, status: :unprocessable_entity }
      end
    end
  end
我得到的错误是:

undefined method `step_url' for #<StepsController:0x007feeb6442198>

在使用嵌套资源时,请按如下方式更新创建操作:

  def create
    @list = List.find(params[:list_id])
    @step = @list.steps.build(step_params) ## Assuming that list has_many steps

    respond_to do |f|
      if @step.save
        ## update the url passed to redirect_to as below
        f.html { redirect_to list_step_url(@list,@step), notice: 'Step was successfully created.' }
        f.json { render action: 'show', status: :created, location: @step }
      else
        f.html { render action: 'new' }
        f.json { render json: @step.errors, status: :unprocessable_entity }
      end
    end
  end
运行
rake routes
查看可用的路由。 作为通往台阶的路线,这场表演看起来像

GET lists/:list_id/steps/:id steps#show
使用带有两个参数的
list\u step\u url
:list\u id和
@step
For:id转到显示步骤页面

GET lists/:list_id/steps/:id steps#show