没有路由匹配{:action =>"编辑",:controller =>" admin / progress"}缺少必需的密钥:[:id]

时间:2016-09-07 14:14:05

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

在尝试为我的管理信息中心创建编辑链接时。我有这个错误... No route matches {:action=>"edit", :controller=>"admin/progresses"} missing required keys: [:id] 你能解释一下我的错误吗?

这是我的rake routes

 admin GET    /admin(.:format)                         admin/dashboard#index
         admin_progresses GET    /admin/progresses(.:format)              admin/progresses#index
                          POST   /admin/progresses(.:format)              admin/progresses#create
       new_admin_progress GET    /admin/progresses/new(.:format)          admin/progresses#new
      edit_admin_progress GET    /admin/progresses/:id/edit(.:format)     admin/progresses#edit
           admin_progress GET    /admin/progresses/:id(.:format)          admin/progresses#show
                          PATCH  /admin/progresses/:id(.:format)          admin/progresses#update
                          PUT    /admin/progresses/:id(.:format)          admin/progresses#update
                          DELETE /admin/progresses/:id(.:format)          admin/progresses#destroy

这是我的观点/ admin / progress / index.html.slim

.container
  .row
    h2 Pepito's Dashboard
    br
    = link_to "New article", new_admin_progress_path, class:'btn btn-success'
    br
    br

    .dashboard
      - @progresses.each do |progress|
        .row 

          .col-xs-2
            h4
            = progress.title.capitalize
          .col-xs-2
            h4
            = progress.date
          .col-xs-5
            h4  
            = truncate(progress.content, length: 90)
          .col-xs-1
            = link_to 'View', progresses_path, class:'btn btn-success'
          .col-xs-1
            = link_to 'Edit', edit_admin_progress_path(@progress), class:'btn btn-primary'
          .col-xs-1
            = link_to 'Destroy', "#", class:'btn btn-danger', method: :delete
        hr

这是我的admin / progresses_controller.rb:

  class Admin::ProgressesController < Admin::ApplicationController

    def index
      @progresses = Progress.all
    end

    def new
      @progress = Progress.new
    end

    def show
      @progress = Progress.find(params[:id])

    end

    def create
       @progress = Progress.new(progress_params)

       respond_to do |format|
         if @progress.save
           unless params[:progress_attachments].nil?
             params[:progress_attachments]['image'].each do |a|
               @progress_attachment = @progress.progress_attachments.create!(:image => a)
             end
           end
           format.html { redirect_to admin_progresses_path, notice: 'Progress was successfully created.' }
         else
           format.html { render action: 'new' }
         end
       end

     end

     def edit
       @progress = Progress.find(params[:id])
     end

     def destroy
        @progress = Progress.find(params[:id])
     end

     def update
      @progress = Progress.find(params[:id])
        if @progress.update(params[:progress].permit(:title, :date, :content, :main_image, progress_attachments_attributes: [:id, :progress_id, :image]))
          redirect_to progresses_path
        else
      render 'edit'
        end
    end


     private

     def progress_params
        params.require(:progress).permit(:title, :content, :date, :main_image, progress_attachments_attributes: [:id, :progress_id, :image])
     end

  end

非常感谢! :)

修改

感谢大家的所有答案!

= link_to 'Edit', edit_admin_progress_path(progress), class:'btn btn-primary'效果很好!!

5 个答案:

答案 0 :(得分:2)

尝试更改

= link_to 'Edit', edit_admin_progress_path(@progress), class:'btn btn-primary'

= link_to 'Edit', edit_admin_progress_path(progress), class:'btn btn-primary'

您要使用的变量不是实例变量,它是

中的局部变量
@progresses.each do |progress|

答案 1 :(得分:0)

new Programmer("JavaScript"){}.writeCode(); // prints "JavaScript" 更改为pjsua_aud.c ..Opening sound device (speaker + mic) PCM@16000/1/20ms alsa_dev.c ...ALSA lib pcm_hw.c:1557:(snd_pcm_hw_open) open '/dev/snd/pcmC0D0c' failed (-16): Device or resource busy pjsua_aud.c ..Unable to open sound device: Unknown error from audio driver (PJMEDIA_EAUD_SYSERR) [status=420002] operation=make_call(), error=Unknown error from audio driver (PJMEDIA_EAUD_SYSERR)

@progress

当您在循环中显示progress链接时,您没有= link_to 'Edit', edit_admin_progress_path(progress), class:'btn btn-primary' 可用,但edit

答案 2 :(得分:0)

由于您使用本地变量@progresses循环遍历progress,因此您应该将progress传递给网址助手。

= link_to 'Edit', edit_admin_progress_path(@progress), class:'btn btn-primary'

应该是

= link_to 'Edit', edit_admin_progress_path(progress), class:'btn btn-primary'

答案 3 :(得分:0)

更改为

edit_admin_progress_path(progress)

@progress是实例变量,如果未设置则只有nil没有任何错误

答案 4 :(得分:0)

尝试放弃&#39; @&#39;在您的修改路径中progress之前 - 在表单构建器中,您使用progress代替@progresses中的每个单独进度,因此您不需要引用该实例@progress在该特定路径中:

link_to 'Edit', edit_admin_progress_path(progress), class:'btn btn-primary'

此外,@progress未在索引视图中设置,此表单是其中的一部分。但是,您可以在修改视图中设置@progress,但是一旦您按照链接进行get请求即可。

如果这不起作用,请告诉我!