1

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

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

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

垃圾控制器.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

垃圾文件夹中的 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 %>

显示.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 %>

当我在本地主机的索引根页面中单击显示时,我不断为 nil:NilClass 获取未定义的方法“名称”。

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

 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
4

1 回答 1

0

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

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

希望这会有所帮助。

于 2013-07-24T19:17:24.407 回答