脚手架导轨应用程序删除显示操作,现在编辑/销毁不再工作

时间:2013-07-24 18:39:35

标签: ruby-on-rails

您好我为我的rails应用程序生成了一个脚手架。我在配置文件中为该模型添加了资源。我想我可以剪切/修改后来生成的脚手架。我删除了show方法/视图等,现在销毁/编辑不再有效。

事实证明,由于资源的原因,破坏/编辑也需要显示在那里。

我想将show重新推送到我的应用程序中以解决此问题,有人可以帮我查明问题吗?

trashes_controller.rb

class TrashesController < ApplicationController
  # GET /trashes
  # GET /trashes.json


  def index
    @trash = Trash.all
    @json = @trash.to_gmaps4rails 
    end
  end

  # GET /trashes/1
  # GET /trashes/1.json

  def show
    @trash = Trash.find(params[:id])

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

  # GET /trashes/new
  # GET /trashes/new.json
  def new
    @trash = Trash.new

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

  # GET /trashes/1/edit
  def edit
    @trash = Trash.find(params[:id])
  end

  # POST /trashes
  # POST /trashes.json
  def create
    @trash = Trash.new(params[:trash])

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

  # PUT /trashes/1
  # PUT /trashes/1.json
  def update
    @trash = Trash.find(params[:id])

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

  # DELETE /trashes/1
  # DELETE /trashes/1.json

  def destroy
    @trash = Trash.find(params[:id])
    @trash.destroy

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

在trashes文件夹中的index.html.erb

<h1>Listing Boston/Cambridge trash bin locations</h1>
<%= gmaps4rails(@json) %>
<table>
  <tr>
    <th>Name</th>
    <th>Address</th>
    <th>Category</th>
    <th></th>
    <th></th>
  </tr>

<% @trash.each do |trash| %>
  <tr>
    <td><%= trash.name %></td>
    <td><%= trash.address %></td>
    <td><%= trash.category %></td>
    <td><%= link_to 'Show', trash %></td>
    <td><%= link_to 'Edit', edit_trash_path(trash) %></td>
    <td><%= link_to 'Destroy', trash, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New Boston Solar Powered Trash Can Location', new_trash_path %>

show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <b>Name:</b>
  <%= @trash.name %>
</p>

<p>
  <b>Address:</b>
  <%= @trash.address %>
</p>

<p>
  <b>Category:</b>
  <%= @trash.category %>
</p>

<%= link_to 'Edit', edit_trash_path(@trash) %> |
<%= link_to 'Back', trashes_path %>

当我在本地主机的索引根页面中单击show时,我继续为nil获取未定义的方法`name':NilClass。

这是我的佣金路线页面,可能更有用。

 trashes GET    /trashes(.:format)          trashes#index
           POST   /trashes(.:format)          trashes#create
 new_trash GET    /trashes/new(.:format)      trashes#new
edit_trash GET    /trashes/:id/edit(.:format) trashes#edit
     trash GET    /trashes/:id(.:format)      trashes#show
           PUT    /trashes/:id(.:format)      trashes#update
           DELETE /trashes/:id(.:format)      trashes#destroy
      root        /                           trashes#index
      page GET    /pages/*id                  high_voltage/pages#show

1 个答案:

答案 0 :(得分:0)

如果您在尝试点击“显示”链接时发生错误,请修改您的link_to路径,如下所示:

td><%= link_to 'Show', trash_path(trash) %></td>

希望这会有所帮助。