Admin 中的 NoMethodError - 命名空间的 Rails 更改 [英] NoMethodError in Admin - Rails change of namespace

查看:50
本文介绍了Admin 中的 NoMethodError - 命名空间的 Rails 更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的让我很烦.我的控制器工作正常,但我现在已经更改了命名空间并且似乎在路径或其他方面存在问题.我尝试根据 rake 路线编辑路径,但仍然无济于事

This is really bugging me. My controller was working fine, but I have now changed the namespace and appear to be having issues with the paths or something. I tried editing the paths in accordance with rake routes but still no avail

这里是错误:

Showing /home/will/Development/Ruby-Files/tasks/app/views/admin/testos/index.html.erb  where line #16 raised:

undefined method `testo_path' for #<#<Class:0xb61ee4f0>:0xaeb2c38>

我的控制器:

class Admin::TestosController < ApplicationController
  # GET /testos
  # GET /testos.json
  def index
    @testos = Testo.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @testos }
    end
  end

  # GET /testos/1
  # GET /testos/1.json
  def show
    @testo = Testo.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @testo }
    end
  end

  # GET /testos/new
  # GET /testos/new.json
  def new
    @testo = Testo.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @testo }
    end
  end

  # GET /testos/1/edit
  def edit
    @testo = Testo.find(params[:id])
  end

  # POST /testos
  # POST /testos.json
  def create
    @testo = Testo.new(params[:testo])

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

  # PUT /testos/1
  # PUT /testos/1.json
  def update
    @testo = Testo.find(params[:id])

    respond_to do |format|
      if @testo.update_attributes(params[:testo])
        format.html { redirect_to @testo, notice: 'Testo was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @testo.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /testos/1
  # DELETE /testos/1.json
  def destroy
    @testo = Testo.find(params[:id])
    @testo.destroy

    respond_to do |format|
      format.html { redirect_to testos_url }
      format.json { head :no_content }
    end
  end
end

还有我的视图文件

列出 testos

<table>
  <tr>
    <th>Title</th>
    <th>Entry</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @testos.each do |testo| %>
  <tr>
    <td><%= testo.title %></td>
    <td><%= testo.entry %></td>
    <td><%= link_to 'Show', testo %></td>
    <td><%= link_to 'Edit', edit_testo_path(testo) %></td>
    <td><%= link_to 'Destroy', testo, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New Testo', new_testo_path %>

我的路由:

namespace :admin do
   resources :testos
end

耙路线:

        root        /                                Pages#index
        admin_testos GET    /admin/testos(.:format)          admin/testos#index
                     POST   /admin/testos(.:format)          admin/testos#create
     new_admin_testo GET    /admin/testos/new(.:format)      admin/testos#new
    edit_admin_testo GET    /admin/testos/:id/edit(.:format) admin/testos#edit
         admin_testo GET    /admin/testos/:id(.:format)      admin/testos#show
                     PUT    /admin/testos/:id(.:format)      admin/testos#update
                     DELETE /admin/testos/:id(.:format)      admin/testos#destroy

推荐答案

您的视图包含非命名空间路由.将这些替换为命名空间的:

Your view contains non-namespaced routes. Substitute these out for namespaced ones:

<td><%= link_to 'Show', admin_testo_path(testo) %></td>
<td><%= link_to 'Edit', edit_admin_testo_path(testo) %></td>

这篇关于Admin 中的 NoMethodError - 命名空间的 Rails 更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆