Ruby on rails 未定义的局部变量或方法“user';”如何引用用户?
我试图在html.erb页面中呈现用户列表,但得到的错误是用户变量未定义 据我所知,为了让视图采用@users变量,需要在链接到视图路由的控制器中调用它。我在下面尝试过,但我认为我调用控制器是错误的(因此定义@users的index方法也是错误的)。我将如何着手修复此问题,以便使用用户列表呈现部分内容 views/pages/contacts.html.erb:Ruby on rails 未定义的局部变量或方法“user';”如何引用用户?,ruby-on-rails,Ruby On Rails,我试图在html.erb页面中呈现用户列表,但得到的错误是用户变量未定义 据我所知,为了让视图采用@users变量,需要在链接到视图路由的控制器中调用它。我在下面尝试过,但我认为我调用控制器是错误的(因此定义@users的index方法也是错误的)。我将如何着手修复此问题,以便使用用户列表呈现部分内容 views/pages/contacts.html.erb: <table class='table table-condensed'> <thead> <
<table class='table table-condensed'>
<thead>
<tr>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<%= render 'contact', collection: @users, as: :user %>
</tbody>
</table>
<%= content_tag :tr, id: dom_id(user) do %>
<td><%= user.email %></td>
<td>
<%= link_to 'Add Contact', [:new, :contactship, id: user], remote: true unless current_user.has_contactship?(user) %>
<%# or link_to 'Add Contact', new_contactship_path(id: user), remote: true %>
<%= link_to 'Accept Request', [:contactships, id: user], method: :post, remote: true if current_user.requested_contacts_with?(user) %>
<%= link_to 'Remove Request', [:contactship, id: user], method: :delete, remote: true if current_user.pending_contacts_with?(user) %>
<%= link_to 'Remove Friend', [:contactship, id: user], method: :delete, remote: true if current_user.contacts_with?(user) %>
</td>
<% end %>
Rails.application.routes.draw do
resources :posts
resources :contactships, only: [:new, :create, :destroy]
devise_for :users
authenticated :user do
root "pages#my_circles", as: :authenticated_root
get "/app/views/pages/to_post.html.erb", to: "pages#to_post", as: "to_post"
get "/app/views/pages/my_vault.html.erb", to: "pages#my_vault", as: "my_vault"
get "/app/views/pages/my_settings.html.erb", to: "pages#my_settings", as: "my_settings"
get "/app/views/pages/contacts.html.erb", to: "pages#contacts", as: "contacts" #THIS ROUTE
end
root 'pages#home'
namespace :api, defaults: { format: :json } do
namespace :v1 do
resources :circles, only: [:index, :show, :create, :update, :destroy]
end
end
end
class PagesController < ApplicationController
def index
@users = User.all
end
控制器/页面\u控制器.rb:
<table class='table table-condensed'>
<thead>
<tr>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<%= render 'contact', collection: @users, as: :user %>
</tbody>
</table>
<%= content_tag :tr, id: dom_id(user) do %>
<td><%= user.email %></td>
<td>
<%= link_to 'Add Contact', [:new, :contactship, id: user], remote: true unless current_user.has_contactship?(user) %>
<%# or link_to 'Add Contact', new_contactship_path(id: user), remote: true %>
<%= link_to 'Accept Request', [:contactships, id: user], method: :post, remote: true if current_user.requested_contacts_with?(user) %>
<%= link_to 'Remove Request', [:contactship, id: user], method: :delete, remote: true if current_user.pending_contacts_with?(user) %>
<%= link_to 'Remove Friend', [:contactship, id: user], method: :delete, remote: true if current_user.contacts_with?(user) %>
</td>
<% end %>
Rails.application.routes.draw do
resources :posts
resources :contactships, only: [:new, :create, :destroy]
devise_for :users
authenticated :user do
root "pages#my_circles", as: :authenticated_root
get "/app/views/pages/to_post.html.erb", to: "pages#to_post", as: "to_post"
get "/app/views/pages/my_vault.html.erb", to: "pages#my_vault", as: "my_vault"
get "/app/views/pages/my_settings.html.erb", to: "pages#my_settings", as: "my_settings"
get "/app/views/pages/contacts.html.erb", to: "pages#contacts", as: "contacts" #THIS ROUTE
end
root 'pages#home'
namespace :api, defaults: { format: :json } do
namespace :v1 do
resources :circles, only: [:index, :show, :create, :update, :destroy]
end
end
end
class PagesController < ApplicationController
def index
@users = User.all
end
class PagesController
谢谢 class PagesControllerclass PagesController < ApplicationController
def index
@users = User.all
end
...
def索引
@users=User.all
结束
...
将contacts.html.erb重命名为index.html.erb
<table class='table table-condensed'>
<thead>
<tr>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<%= render partial: 'contact', collection: @users, as: :user %>
</tbody>
</table>
index.html.erb
<table class='table table-condensed'>
<thead>
<tr>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<%= render partial: 'contact', collection: @users, as: :user %>
</tbody>
</table>
电子邮件
行动
请在视图/页面中发布index.html.erb视图,这是将由页面控制器索引操作呈现的文件嘿,詹姆斯,我没有index.html.erb视图。我要呈现的页面是contacts.html.erb,如果我使用get/app/views/pages/contacts.html.erb”,则“pagesාindex”,改为“contacts”
在我的路由中?运行rails服务器的终端中显示的输出,因此您的日志文件将显示正在使用哪些参数处理控制器操作(如果有的话),只需确认您看到类似于Started get…
的内容,然后是processing by PagesController#index as HTML
控制器操作负责呈现视图。Rails magic将在以控制器命名的相关视图文件夹中自动呈现以操作命名的视图。如果您想要呈现不同的视图,那么索引操作需要调用render方法render'contacts'
您正在使用Rails,因此无法自动利用Rails功能,但这还不止于此。index.html文件在任何web应用程序的任何文件夹中都是标准的,而不仅仅是Railso。您的URL中的路径将更有意义