0

我是 Rails 的新手,正在努力在“ views/admins/show.html.erb ”中创建一个超链接,以加载“ views/residents/new.html.erb ”。以清晰的名义,“ views/admins/show.html.erb ”和“ views/residents/new.html.erb ”来自不同的控制器。我很难找到路由失败的解决方案,并且正在生成以下消息:

管理员中的名称错误#show

显示 /Users/beracus/rails_projects/whizcharts/app/views/admins/show.html.erb 其中第 11 行提出:

未定义的局部变量或方法“residents_new”

<#:0x000001019a2228> 提取的源代码(第 11 行附近):

11: <%= link_to '创建一个新居民',residents_new %>

我想弄清楚如何在 Rails 中成功创建一个超链接,使我能够链接到其他视图/部分,无论它们是否来自同一个控制器。此外,我想更好地了解我的错误消息意味着什么,以帮助防止将来发生这种情况。感谢您对文档的任何指导,并指出我可能违反的任何设计规则。我已经搜索并发现了对其他人构成的类似挑战,但由于我缺乏经验,我还无法满足我的需求的这些解决方案。

我尝试了以下方法。

  1. Ruby on Rails 指南
  2. Rails 3 中的低层路线
  3. Rails 3 从另一个控制器渲染动作
  4. 未定义的方法“companies_path”错误
  5. ruby rails - 未定义的局部变量或方法`new_user_session_path'

这是我的代码:

    # config/routes.rb
     Sample::Application.routes.draw do
      resources :admins do
        resources :residents
      end
      resources :sessions, only: [:new, :create, :destroy]

      root to: 'static_pages#home'

      match '/signup',    to: 'admins#new'
      match '/signin',    to: 'sessions#new'
      match '/signout',   to: 'sessions#destroy', via: :delete

      match '/help',      to: 'static_pages#help'
      match '/about',     to: 'static_pages#about'
      match '/contact',   to: 'static_pages#contact'
          .
          .
          .
      match ':controller(/:action(/:id))(.:format)'
    end


    # controllers/admins_controller.rb

    class AdminsController < ApplicationController
    before_filter :signed_in_admin, only: [:index, :edit, :update]
    before_filter :correct_admin,   only: [:edit, :update]
    before_filter :super_admin, only: :destroy

    def index
        @admins = Admin.paginate(page: params[:page]) 
    end

    def show
        @admin = Admin.find(params[:id])
    end

    def new
        @admin = Admin.new
    end

    def create
        @admin = Admin.new(params[:admin])
        if @admin.save
            sign_in @admin
            flash[:success] = "Welcome to Whizcharts!"
            redirect_to @admin
        else
            render 'new'
        end
    end

    def edit
        @admin = Admin.find(params[:id])
    end

    def update
        @admin = Admin.find(params[:id])
        if @admin.update_attributes(params[:admin])
            flash[:success] = "Profile updated"
            sign_in @admin
            redirect_to @admin
        else
            render 'edit'
        end
    end

    def destroy
        Admin.find(params[:id]).destroy
        flash[:success] = "User deleted."
        redirect_to admins_path
    end

    private

        def signed_in_admin
            unless signed_in?
            store_location
            redirect_to signin_path, notice: "Please sign in."
        end

        def correct_admin
            @admin = Admin.find(params[:id])
            redirect_to(root_path) unless current_admin?(@admin)
        end

        def super_admin
            redirect_to(root_path) unless current_admin.super?
        end
    end
end


# controllers/residents_controller.rb

class ResidentsController < ApplicationController
  def index
    @residents = Resident.paginate(page: params[:page])
  end

  def show
    @resident = Resident.find(params[:id])
  end

  def new
    @resident = Resident.new
  end

  def create
    @resident = Resident.new(params[:resident])
  end

  def edit
    @resident = Resident.find(params[:id])
  end

  def update
    @resdient = Resident.find(params[:id])
    if @resident.update_attributes(params{:resident})
        flash[:success] = "Resident's profile updated"
        sign_in @resident
        redirect_to @resident
    else
      render 'edit'
    end
  end

  def destroy
    Resident.find(params[:id]).destroy
    flash[:success] = "Resident deleted"
    redirect_to residents_path
  end

  def _form
    @residents = Resident.paginate(page: params[:page])
  end
end


# views/admins/show.html.erb

<% provide(:title, @admin.fname + " " + @admin.lname) %>
<div class="row">
    <aside class="span4">
        <section>
            <h1>
                <%= gravatar_for @admin %>
                <%= @admin.fname + " " + @admin.lname %> 
            </h1>
        </section>
        <section class="resident">
            <%= link_to 'create a new resident', residents_new %>
        </section>
    </aside>
</div>

# views/residents/new.html.erb
<% provide(:title, @admin.fname + " " + @admin.lname) %>
<div class="row">
    <aside class="span4">
        <section>
            <h1>
                <%= gravatar_for @admin %>
                <%= @admin.fname + " " + @admin.lname %> 
            </h1>
        </section>
        <section class="resident">
            <%= link_to 'create a new resident', residents_new %>
        </section>
    </aside>
</div>

# views/residents/form.html.erb 

<%= form_for(@resident) do |f| %>
    <% if @resident.error.any? %>
        <div id="error_explanation">
            <h2>
                <%= pluralize(@resident.errors.count, "error") %>
                prohibited this resident from being saved:
            </h2>
            <ul>
                <% @resident.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
                <% end %>
            </ul>
        </div>
    <% end %>

    <div class="field">
        <%= f.label :fname %><br />
        <%= f.text_field :fname %>
    </div>
    <div class="field">
        <%= f.label :lname %><br />
        <%= f.text_field :lname %>
    </div>
    <div class="field">
        <%= f.label :dob %><br />
        <%= f.text_field :dob %>
    </div>
    <div class="field">
        <%= f.radio_button :gender, 'Male' %>
        <%= f.label "gender", "Male" %>
        <br />
        <%= f.radio_button :gender, 'Female' %>
        <%= f.label "gender", "Female" %>
        <br />
        <%= f.radio_button :gender, 'Other' %>
        <%= f.label "gender", "Other" %>
        <br />
    </div>
    <div class="field">
        <%= f.label :soc %><br />
        <%= f.text_field :soc %><br />
    </div>
    <div class="field">
        <%= f.label :address %><br />
        <%= f.text_field :address %>
    </div>
    <div class="field">
        <%= f.label :city %><br />
        <%= f.text_field :city %>
    </div>
    <div class="field">
        <%= f.label :state %><br />
        <%= f.text_field :state %>
    </div>
    <div class="field">
        <%= f.label :zip %><br />
        <%= f.text_field :zip %>
    </div>
    <div class="field">
        <%= f.label :phone %><br />
        <%= f.text_field :phone %>
    </div>
    <div class="field">
        <%= f.label :doc_fname %><br />
        <%= f.text_field :doc_fname %>
    </div>
    <div class="field">
        <%= f.label :doc_lname %><br />
        <%= f.text_field :doc_lname %>
    </div>
    <div class="field">
        <%= f.label :doc_phone1 %><br />
        <%= f.text_field :doc_phone1 %>
    </div>
    <div class="field">
        <%= f.label :doc_phone2 %><br />
        <%= f.text_field :doc_phone2 %>
    </div>
    <div class="field">
        <%= f.label :doc_fax %><br />
        <%= f.text_field :doc_fax %>
    </div>
    <div class="field">
        <%= f.label :doc_email %><br />
        <%= f.text_field :doc_email %>
    </div>
    <div class="field">
        <%= f.label :guard_fname %><br />
        <%= f.text_field :guard_fname %>
    </div>
    <div class="field">
        <%= f.label :guard_lname %><br />
        <%= f.text_field :guard_lname %>
    </div>
    <div class="field">
        <%= f.label :guard_address %><br />
        <%= f.text_field :guard_address %>
    </div>
    <div class="field">
        <%= f.label :guard_city %><br />
        <%= f.text_field :guard_city %>
    </div>
    <div class="field">
        <%= f.label :guard_state %><br />
        <%= f.text_field :guard_state %>
    </div>
    <div class="field">
        <%= f.label :guard_zip %><br />
        <%= f.text_field :guard_zip %><br />
    </div>
    <div class="field">
        <%= f.label :guard_phone1 %><br />
        <%= f.text_field :guard_phone1 %>
    </div>
    <div class="field">
        <%= f.label :guard_phone2 %><br />
        <%= f.text_field :guard_phone2 %>
    </div>
    <div class="field">
        <%= f.label :guard_email %><br />
        <%= f.text_field :guard_email %>
    </div>
    <div class="actions">
        <%= f.submit %>
    </div>
    <% end %>


# rake routes 

Mac-Pro:whizcharts beracus$ rake routes
    admin_residents GET    /admins/:admin_id/residents(.:format)          residents#index
                    POST   /admins/:admin_id/residents(.:format)          residents#create
 new_admin_resident GET    /admins/:admin_id/residents/new(.:format)      residents#new
edit_admin_resident GET    /admins/:admin_id/residents/:id/edit(.:format) residents#edit
     admin_resident GET    /admins/:admin_id/residents/:id(.:format)      residents#show
                    PUT    /admins/:admin_id/residents/:id(.:format)      residents#update
                    DELETE /admins/:admin_id/residents/:id(.:format)      residents#destroy
             admins GET    /admins(.:format)                              admins#index
                    POST   /admins(.:format)                              admins#create
          new_admin GET    /admins/new(.:format)                          admins#new
         edit_admin GET    /admins/:id/edit(.:format)                     admins#edit
              admin GET    /admins/:id(.:format)                          admins#show
                    PUT    /admins/:id(.:format)                          admins#update
                    DELETE /admins/:id(.:format)                          admins#destroy
           sessions POST   /sessions(.:format)                            sessions#create
        new_session GET    /sessions/new(.:format)                        sessions#new
            session DELETE /sessions/:id(.:format)                        sessions#destroy
               root        /                                              static_pages#home
             signup        /signup(.:format)                              admins#new
             signin        /signin(.:format)                              sessions#new
            signout DELETE /signout(.:format)                             sessions#destroy
               help        /help(.:format)                                static_pages#help
              about        /about(.:format)                               static_pages#about
            contact        /contact(.:format)                             static_pages#contact
                           /admin(.:format)                               admins#index
               show        /show(.:format)                                admins#show
                new        /new(.:format)                                 admins#new
             create        /create(.:format)                              admins#create
               edit        /edit(.:format)                                admins#edit
             update        /update(.:format)                              admins#update
            destroy        /destroy(.:format)                             admins#destroy
          residents        /residents(.:format)                           residents#path
               form        /form(.:format)                                residents#form
                           /create(.:format)                              residents#create
                           /destroy(.:format)                             residents#destroy
                           /edit(.:format)                                residents#edit
              index        /index(.:format)                               residents#index
                           /new(.:format)                                 residents#new
                           /show(.:format)                                residents#show
                           /update(.:format)                              residents#update
                           /:controller(/:action(/:id))(.:format)         :controller#:action
4

1 回答 1

0

真的有必要match为管理员和居民使用所有这些config/routes.rb吗?

您已经创建了足智多谋的路线

resources :admins do 
  resources :residents
end

无论如何new_admin_resident_path,助手应该解决您的嵌套资源问题。或未new_resident_path嵌套的助手。

<%= link_to 'Create a new resident', new_admin_resident_path %>
于 2012-09-10T20:54:29.023 回答