Rails 5 - 路由和显示页面未连接
Rails 5 - routes and show page not hooking up
我正在尝试为 Rails 5 中的人员列表设置显示页面,但无法同步路线。
config.route:
get '/team', to: 'pages#team'
get '/team/:id', to: 'pages#show', as: 'agent' #to get agent_path from rails routes
page_controller:
def team
@admins = Admin.where(role: :staff)
end
def show
@admins = Admin.find(params[:id])
end
views/pages/team.html.erb:
<% @admins.each do |staff| %>
<div class="team-link__inner">
<div class="agent-image-team">
<%= link_to (image_tag(staff.url, class: "team_link") agent_path(staff.id), class: "team-link") %>
</div>
<div class="hover-team-info">
<p><%= staff.name %></p>
</div>
</div>
<% end %>
views/pages/show.html.erb:(样本以确认路线)
<h1>Admin show page</h1>
<%@admins.each do |staff| %>
<p><% staff.name %></p>
<% end %>
我尝试过以多种不同的方式连接我的展示页面,但是当我尝试在索引(团队)视图中设置我的 link 时,总是出现错误。我做错了什么?
错误是:
.../app/views/pages/team.html.erb:26: syntax error, unexpected tIDENTIFIER, expecting ')' class: "team_link") agent_path(staff.id), class: "team-link" ^
.../app/views/pages/team.html.erb:26: syntax error, unexpected ',', expecting ')' m_link") agent_path(staff.id), class: "team-link") );@output ^
.../app/views/pages/team.html.erb:26: syntax error, unexpected ')', expecting keyword_end aff.id), class: "team-link") );@output_buffer.safe_append=' ^
.../app/views/pages/team.html.erb:60: syntax error, unexpected keyword_ensure, expecting end-of-input ensure ^
show route用于显示单个对象的信息
def show
@admin = Admin.find(params[:id])
end
并且您的视图应该只显示该特定对象属性的信息 (show.html.erb)
<h1>Admin show page</h1>
<p><%= @admin.name %></p>
根据错误,您在图像标记右括号后缺少逗号
<%= link_to(image_tag(staff.url, class: "team_link"), agent_path(staff.id), class: "team-link") %>
你的link_to声明是有问题的。尝试:
<%= link_to agent_path(staff.id), class: "team-link" do %>
<%= image_tag staff.url, class: "team_link" %>
# or
<img src="#{staff.url}" class="team_link" />
<% end %>
希望它能解决错误!
我正在尝试为 Rails 5 中的人员列表设置显示页面,但无法同步路线。
config.route:
get '/team', to: 'pages#team'
get '/team/:id', to: 'pages#show', as: 'agent' #to get agent_path from rails routes
page_controller:
def team
@admins = Admin.where(role: :staff)
end
def show
@admins = Admin.find(params[:id])
end
views/pages/team.html.erb:
<% @admins.each do |staff| %>
<div class="team-link__inner">
<div class="agent-image-team">
<%= link_to (image_tag(staff.url, class: "team_link") agent_path(staff.id), class: "team-link") %>
</div>
<div class="hover-team-info">
<p><%= staff.name %></p>
</div>
</div>
<% end %>
views/pages/show.html.erb:(样本以确认路线)
<h1>Admin show page</h1>
<%@admins.each do |staff| %>
<p><% staff.name %></p>
<% end %>
我尝试过以多种不同的方式连接我的展示页面,但是当我尝试在索引(团队)视图中设置我的 link 时,总是出现错误。我做错了什么?
错误是:
.../app/views/pages/team.html.erb:26: syntax error, unexpected tIDENTIFIER, expecting ')' class: "team_link") agent_path(staff.id), class: "team-link" ^
.../app/views/pages/team.html.erb:26: syntax error, unexpected ',', expecting ')' m_link") agent_path(staff.id), class: "team-link") );@output ^
.../app/views/pages/team.html.erb:26: syntax error, unexpected ')', expecting keyword_end aff.id), class: "team-link") );@output_buffer.safe_append=' ^
.../app/views/pages/team.html.erb:60: syntax error, unexpected keyword_ensure, expecting end-of-input ensure ^
show route用于显示单个对象的信息
def show
@admin = Admin.find(params[:id])
end
并且您的视图应该只显示该特定对象属性的信息 (show.html.erb)
<h1>Admin show page</h1>
<p><%= @admin.name %></p>
根据错误,您在图像标记右括号后缺少逗号
<%= link_to(image_tag(staff.url, class: "team_link"), agent_path(staff.id), class: "team-link") %>
你的link_to声明是有问题的。尝试:
<%= link_to agent_path(staff.id), class: "team-link" do %>
<%= image_tag staff.url, class: "team_link" %>
# or
<img src="#{staff.url}" class="team_link" />
<% end %>
希望它能解决错误!