【问题标题】:Rails app generating wrong :id in parent controllerRails 应用程序在父控制器中生成错误:id
【发布时间】:2012-07-10 14:16:31
【问题描述】:

我第一次创建了一个非常基本的 Rails 应用程序,其中包含 2 个资源,部门(部门)和成员。我相信我已经正确使用了嵌套资源,但是由于某种原因,在运行 rails server 之后,父资源的 :id 没有正确生成/传递。 Root 是 depts#index,从这里我可以使用在新视图和编辑视图中呈现的 _form.haml 进行新建和编辑。但是,当我执行 /depts/3 时,出现“找不到 id=3 的部门”的错误。单击以从索引进行编辑会在 URL 中给我 /depts/63/edit - 我不确定这个 id=63 来自哪里。尝试通过在 URL 中键入 /dept/63 来执行“显示”操作不起作用。起初我自己创建了 Depts,让它与所有动作和视图一起工作,自从我添加了成员​​资源后出现了问题。

routes.rb

        resources :depts do
  resources :members
end

depts_controller.rb

    def index
        @depts = Dept.all

        respond_to do |format|
          format.html
          format.json { render :json => @depts }
        end
      end

      def show
        @dept = Dept.find(params[:dept_id])

        respond_to do |format|
          format.html
          format.json { render :json => @dept }
        end
      end

      def new
        @dept = Dept.new(params[:dept])

        respond_to do |format|
          format.html
          format.json { render :json => @dept }
        end
      end

      def create
        @dept = Dept.new(params[:dept])

        respond_to do |format|
          if @dept.save
             format.html { redirect_to :action => 'index' }
             format.json { render :json => @dept }
          else
             format.html { render :action => 'new' }
             format.json { render :json => @dept }
          end
        end
      end

      def edit
        @dept = Dept.find(params[:id])
      end

      def update
        @dept = Dept.find(params[:id])

        respond_to do |format|
          if @dept.update_attributes(params[:dept])
             format.html { redirect_to :action => 'index'}#, :id => @dept }
             format.json { render :json => @dept }

          else
             format.html { redirect_to :action => 'edit' }
             format.json { render :json => @dept }
          end
        end
      end

    def destroy
        @dept = Dept.find(params[:id])
        @dept.destroy

        respond_to do |format|
          format.html { redirect_to :action => 'index' }
          format.json { render :json => @dept }
        end
      end
    end

show.haml

    %p= @dept.name

    %p= link_to "back", {:action => 'index'}

index.haml

    %h1 DEPARTMENTS

    %ol
      - @depts.each do |d|
        %li= link_to d.name
        %p= link_to 'edit department', edit_dept_path(d)
        %p= link_to 'get rid of department!', d, :method => :delete, :id => d.id

      %br
    %p= link_to "ADD A NEW DEPARTMENT", new_dept_path

【问题讨论】:

    标签: ruby-on-rails-3 controller nested-resources


    【解决方案1】:

    在显示方法更改中:

    @dept = Dept.find(params[:dept_id])
    

    到:

    @dept = Dept.find(params[:id])
    

    并在新方法更改中:

    @dept = Dept.new(params[:dept])
    

    只是:

    @dept = Dept.new
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      相关资源
      最近更新 更多