Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing link_to for creating a link to a view from a different controller
    text
    copied!<p>I'm new to Rails and am working towards creating a hyper link in '<em>views/admins/show.html.erb</em>' that loads '<em>views/residents/new.html.erb</em>'. In the name of clarity '<em>views/admins/show.html.erb</em>' and '<em>views/residents/new.html.erb</em>' are from separate controllers. I am stumped on finding solutions to my routing failures, and am generating the following message:</p> <blockquote> <p>NameError in Admins#show</p> <p>Showing /Users/beracus/rails_projects/whizcharts/app/views/admins/show.html.erb where line #11 raised:</p> <p>undefined local variable or method `residents_new' for</p> <h1>&lt;#:0x000001019a2228> Extracted source (around line #11):</h1> <p>11: &lt;%= link_to 'create a new resident', residents_new %> </p> </blockquote> <p>I would like to figure out how to successfully create a hyper-link in Rails that enables me to link to other views/partials whether or not they are from the same controller. Also I would like to better understand what my error message means to help prevent this in the future. Any guidance to documentation is appreciated, as well as pointing out any design rules I may be violating. I've searched for and found similar challenges posed to others, but due to my inexperience, I've not yet been able to cater those solutions to my needs.</p> <p>I have tried the following.</p> <ol> <li><a href="http://guides.rubyonrails.org/getting_started.html#adding-a-link" rel="nofollow noreferrer">Ruby on Rails guide</a></li> <li><a href="http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/" rel="nofollow noreferrer">The low down on routes in Rails 3</a></li> <li><a href="https://stackoverflow.com/questions/6051776/rails-3-render-action-from-another-controller">Rails 3 render action from another controller</a></li> <li><a href="https://stackoverflow.com/questions/8307573/undefined-method-companies-path-error">undefined method `companies_path' error</a></li> <li><a href="https://stackoverflow.com/questions/9850244/ruby-rails-undefined-local-variable-or-method-new-user-session-path?rq=1">ruby rails - undefined local variable or method `new_user_session_path'</a></li> </ol> <p>Here's my code:</p> <pre><code> # 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 &lt; 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 &lt; 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 &lt;% provide(:title, @admin.fname + " " + @admin.lname) %&gt; &lt;div class="row"&gt; &lt;aside class="span4"&gt; &lt;section&gt; &lt;h1&gt; &lt;%= gravatar_for @admin %&gt; &lt;%= @admin.fname + " " + @admin.lname %&gt; &lt;/h1&gt; &lt;/section&gt; &lt;section class="resident"&gt; &lt;%= link_to 'create a new resident', residents_new %&gt; &lt;/section&gt; &lt;/aside&gt; &lt;/div&gt; # views/residents/new.html.erb &lt;% provide(:title, @admin.fname + " " + @admin.lname) %&gt; &lt;div class="row"&gt; &lt;aside class="span4"&gt; &lt;section&gt; &lt;h1&gt; &lt;%= gravatar_for @admin %&gt; &lt;%= @admin.fname + " " + @admin.lname %&gt; &lt;/h1&gt; &lt;/section&gt; &lt;section class="resident"&gt; &lt;%= link_to 'create a new resident', residents_new %&gt; &lt;/section&gt; &lt;/aside&gt; &lt;/div&gt; # views/residents/form.html.erb &lt;%= form_for(@resident) do |f| %&gt; &lt;% if @resident.error.any? %&gt; &lt;div id="error_explanation"&gt; &lt;h2&gt; &lt;%= pluralize(@resident.errors.count, "error") %&gt; prohibited this resident from being saved: &lt;/h2&gt; &lt;ul&gt; &lt;% @resident.errors.full_messages.each do |msg| %&gt; &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;div class="field"&gt; &lt;%= f.label :fname %&gt;&lt;br /&gt; &lt;%= f.text_field :fname %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :lname %&gt;&lt;br /&gt; &lt;%= f.text_field :lname %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :dob %&gt;&lt;br /&gt; &lt;%= f.text_field :dob %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.radio_button :gender, 'Male' %&gt; &lt;%= f.label "gender", "Male" %&gt; &lt;br /&gt; &lt;%= f.radio_button :gender, 'Female' %&gt; &lt;%= f.label "gender", "Female" %&gt; &lt;br /&gt; &lt;%= f.radio_button :gender, 'Other' %&gt; &lt;%= f.label "gender", "Other" %&gt; &lt;br /&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :soc %&gt;&lt;br /&gt; &lt;%= f.text_field :soc %&gt;&lt;br /&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :address %&gt;&lt;br /&gt; &lt;%= f.text_field :address %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :city %&gt;&lt;br /&gt; &lt;%= f.text_field :city %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :state %&gt;&lt;br /&gt; &lt;%= f.text_field :state %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :zip %&gt;&lt;br /&gt; &lt;%= f.text_field :zip %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :phone %&gt;&lt;br /&gt; &lt;%= f.text_field :phone %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :doc_fname %&gt;&lt;br /&gt; &lt;%= f.text_field :doc_fname %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :doc_lname %&gt;&lt;br /&gt; &lt;%= f.text_field :doc_lname %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :doc_phone1 %&gt;&lt;br /&gt; &lt;%= f.text_field :doc_phone1 %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :doc_phone2 %&gt;&lt;br /&gt; &lt;%= f.text_field :doc_phone2 %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :doc_fax %&gt;&lt;br /&gt; &lt;%= f.text_field :doc_fax %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :doc_email %&gt;&lt;br /&gt; &lt;%= f.text_field :doc_email %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :guard_fname %&gt;&lt;br /&gt; &lt;%= f.text_field :guard_fname %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :guard_lname %&gt;&lt;br /&gt; &lt;%= f.text_field :guard_lname %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :guard_address %&gt;&lt;br /&gt; &lt;%= f.text_field :guard_address %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :guard_city %&gt;&lt;br /&gt; &lt;%= f.text_field :guard_city %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :guard_state %&gt;&lt;br /&gt; &lt;%= f.text_field :guard_state %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :guard_zip %&gt;&lt;br /&gt; &lt;%= f.text_field :guard_zip %&gt;&lt;br /&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :guard_phone1 %&gt;&lt;br /&gt; &lt;%= f.text_field :guard_phone1 %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :guard_phone2 %&gt;&lt;br /&gt; &lt;%= f.text_field :guard_phone2 %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :guard_email %&gt;&lt;br /&gt; &lt;%= f.text_field :guard_email %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; # 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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload