加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-4 – 未定义的方法[model] _url

发布时间:2020-12-16 19:22:01 所属栏目:百科 来源:网络整理
导读:我正在处理嵌套资源项目,并在我的步骤控制器中收到错误: 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: :cr
我正在处理嵌套资源项目,并在我的步骤控制器中收到错误:
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>

我的路线看起来像这样:

root 'lists#index'

resources :lists do
  resources :steps
end

解决方法

在使用嵌套资源时,请更新create操作,如下所示:
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),status: :unprocessable_entity }
    end
  end
end

运行rake路线以查看可用路线.
步骤#show的路线看起来像

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

使用list_step_url和2个参数@list For:list_id和@step for:id to to show page of Step.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读