使用link_to创建来自不同控制器的视图的链接

时间:2012-09-10 20:47:15

标签: routes ruby-on-rails-3.2

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

  

Admins中的NameError#show

     

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

     

未定义的局部变量或方法`resident_new'

     

<#:0x000001019a2228>提取的来源(第11行):

     

11:             <%= link_to'创建新居民',resident_new%>

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

我尝试了以下内容。

  1. Ruby on Rails guide
  2. The low down on routes in Rails 3
  3. Rails 3 render action from another controller
  4. undefined method `companies_path' error
  5. ruby rails - undefined local variable or method `new_user_session_path'
  6. 这是我的代码:

        # 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
    

1 个答案:

答案 0 :(得分: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 %>