目前,我尝试做以下工作:
我为我的用户创建了几个部分(即_show_signature.html.erb)。现在,我想在单击链接时显示它们。在我的用户控制器中,我创建了一个新的操作:
def show_signature
@is_on_show_signature = true
end
def show_information
@is_on_show_information = true
end在我的用户show.html.erb上,我编写了以下代码:
<% if @is_on_show_information %>
<%= render :partial => 'show_information' %>
<% elsif @is_on_show_signature %>
<%= render :partial => 'show_signature' %>
<% end %>在我的“导航栏”中我写道:
<ul>
<li class="profile-tab">
<%= link_to 'Information', show_information_path %>
</li>
<li class="profile-tab">
<%= link_to 'Signature', show_signature_path %>
</li>
</ul>在我的routes.rb中,我写道:
map.show_information '/user-information', :controller => 'user', :action => 'show_information'
map.show_signature '/user-signature', :controller => 'user', :action => 'show_signature'现在我的问题是:
单击我的“信息”链接会将我重定向到http://localhost:3000/user-information (因为我在routes.rb中告诉了他以下路径--我想),然后我会得到一个错误:
uninitialized constant UserController但那不是我想要的..。我的用户show path类似于:
http://localhost:3000/users/2-loginname
(通过编码
def to_param
"#{id}-#{login.downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/,'-')
end在我的用户模型中)
我想链接到像http://localhost:3000/users/2-test/user-information这样的智囊团。你知道它是如何工作的吗?你知道为什么我会得到这个错误吗?
发布于 2010-01-08 23:49:39
就Rails约定而言,模型本身是单一的(用户),但是表(用户)和控制器(UsersController)都是多元的。这在一开始可能会造成很大的混乱,即使在使用Rails多年之后,我仍然会尝试像“user=Users.first”这样的东西,这当然是无效的,因为你经常会想到表名而不是类名。
此外,为了切换页面上元素的显示,您可能希望使用link_to_remote方法,该方法使用AJAX进行更新,而不是页面刷新。如果您可以刷新整个页面,这些操作将需要redirect_to一些东西,如页面引用,否则您将得到一个空白页面或错误,因为页面模板不存在。
通常情况下,您要做的是:
<ul>
<li class="profile-tab">
<%= link_to_remote 'Information', show_information_path %>
</li>
<li class="profile-tab">
<%= link_to_remote 'Signature', show_signature_path %>
</li>
</ul>然后,每个操作都与您指定的操作相同,但是,页面模板show_information.rjs将如下所示:
page.replace_html('extra_information', :partial => 'show_information')请记住,您将需要一个占位符来接收部分内容,因此只需将可选部分封装在具有特定ID的元素中:
<div id="extra_information">
<% if @is_on_show_information %>
<%= render :partial => 'show_information' %>
<% elsif @is_on_show_signature %>
<%= render :partial => 'show_signature' %>
<% end %>
</div>https://stackoverflow.com/questions/2028448
复制相似问题