Rails:链接到部分?

目前我尝试做以下事情:

我为我的用户创建了几个部分(即_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我编码:

   'show_information' %>   'show_signature' %>  

在我的“导航栏”中,我写道:

  

在我的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 

但这不是我想要的……我的用户显示路径是这样的:

HTTP://本地主机:3000 /用户/ 2登录名

(通过编码

  def to_param "#{id}-#{login.downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/,'-') end 

在我的用户模型中)

我想链接到像http:// localhost:3000 / users / 2-test / user-information这样的思考 。 任何想法如何工作? 有什么想法我得到这个错误?

就Rails约定而言,模型本身是单数(User),但表(用户)和控制器(UsersController)都是复数。 这可能会导致最初的混乱,甚至在使用Rails多年之后,我仍然会犯下像’user = Users.first’那样的错误,这当然是无效的,因为通常你会想到关于表名而不是类名。

此外,为了切换页面上元素的显示,您可能希望使用link_to_remote方法,该方法使用AJAX进行更新而不是页面刷新。 如果您对完整页面刷新没有问题,那么这些操作将需要重定向到某些内容,例如页面引用者,否则您将获得空白页面或错误,因为页面模板不存在。

通常你做的是:

 
  • <%= link_to_remote 'Information', show_information_path %>
  • <%= link_to_remote 'Signature', show_signature_path %>

然后,每个操作都是您指定的,但是,页面模板show_information.rjs看起来像:

 page.replace_html('extra_information', :partial => 'show_information') 

请记住,您需要一个占位符来接收部分内容,因此只需将可选部分包含在具有特定ID的元素中:

 
<% if @is_on_show_information %> <%= render :partial => 'show_information' %> <% elsif @is_on_show_signature %> <%= render :partial => 'show_signature' %> <% end %>