0

我正在尝试学习如何使用“祖先”宝石。我在 Railscast 中观看了相关的情节并获得了代码。该message表工作正常。但我试图用一个名为locations. 我已经复制并更改了所有代码的名称(我相信)。

我创建了第一个位置。但是,当我转到 时localhost:3000/locations,我得到undefined method了 NilClass:Class for_form.html.erb` 行的 model_name':

<%= form_for @location do |f| %>

routes.rb

  Messenger::Application.routes.draw do
    resources :locations
    resources :messages
    root :to => 'messages#index'
  end

_form.html.erb

<%= form_for @location do |f| %>
  <%= f.error_messages %>
  <%= f.hidden_field :parent_id %>
  <p>
  <%= f.label :locname, "New Location" %><br />
  <%= f.text_area :locname, :rows => 8 %>
  </p>
  <p><%= f.submit "Post Location" %></p>
<% end %>

index.html.erb

<% title "Locations" %>
<%= nested_locations @locations.arrange(:order => :created_at) %>
<%= render "form" %>

locations_controller.rb

class LocationsController < ApplicationController
  def index
    @locations = Location.scoped
  @location = Location.new
end

def show
  @location = Location.find(params[:id])
end

def new
  @location = Location.new(:parent_id => params[:parent_id])
end

def create
  @location = Location.new(params[:location])
  if @location.save
    redirect_to locations_url
  else
  render :new
  end
end

def destroy
  @location = Location.find(params[:id])
  @location.destroy
  redirect_to locations_url
end

结尾

locations_helper.rb

 module LocationsHelper
  def nested_locations(locations)
    locations.map do |location, sub_locations|
      render(location) + content_tag(:div, nested_locations(sub_locations), :class => "nested_locations")
   end.join.html_safe
 end
end
4

0 回答 0