Rails:链接到部分? [英] Rails: Link to partials?

查看:59
本文介绍了Rails:链接到部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我尝试执行以下操作:

我为我的用户创建了几个部分(即 _show_signature.html.erb).现在我想在单击链接时显示它们.在我的用户控制器中,我创建了一个新操作:

 def show_signature@is_on_show_signature = 真结尾def show_information@is_on_show_information = 真结尾

在我的用户 show.html.erb 上,我对此进行了编码:

<% if @is_on_show_information %><%=渲染:部分=>'show_information' %><% elsif @is_on_show_signature %><%=渲染:部分=>'show_signature'%><%结束%>

在我的导航栏"中我写道:

 
    <li class="profile-tab"><%= link_to '信息', show_information_path %><li class="profile-tab"><%= link_to '签名', show_signature_path %>

在我的 routes.rb 中我写道:

 map.show_information '/user-information', :controller =>'用户', :action =>'show_information'map.show_signature '/user-signature', :controller =>'用户', :action =>'show_signature'

现在我的问题:

单击我的信息"链接会将我重定向到 http://localhost:3000/user-information(因为我在 routes.rb 中告诉他这条路径 - 我认为)并且我收到一个错误:

未初始化的常量 UserController

但这不是我想要的...我的用户显示路径类似于:

http://localhost:3000/users/2-loginname

(通过编码

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

在我的用户模型中)

我想链接到类似 http://localhost:3000/users/2-测试/用户信息.任何想法它将如何工作?任何想法为什么我会收到此错误?

解决方案

就 Rails 约定而言,模型本身是单数 (User),但表 (users) 和控制器 (UsersController) 都是复数.一开始这可能会引起很大的混乱,即使在使用 Rails 多年之后,我仍然会犯这样的错误,即尝试诸如user = Users.first"之类的东西,这当然是无效的,因为您经常会想到关于表名而不是类名.

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

通常你要做的是:

    <li class="profile-tab"><%= link_to_remote '信息', show_information_path %><li class="profile-tab"><%= link_to_remote '签名', show_signature_path %>

然后每个动作都按照您指定的方式进行,但是,页面模板 show_information.rjs 将如下所示:

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

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

<% 如果@is_on_show_information %><%=渲染:部分=>'show_information' %><% elsif @is_on_show_signature %><%=渲染:部分=>'show_signature'%><%结束%>

At the moment I try to do following:

I created several partials (i.e. _show_signature.html.erb) for my user. Now I want to show them on clicking a link. In my user controller, I created a new action:

  def show_signature
     @is_on_show_signature = true
  end

  def show_information
     @is_on_show_information = true
  end

on my user show.html.erb i coded this:

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

and in my "navigationbar" i wrote:

  <ul>
    <li class="profile-tab">
      <%= link_to 'Information', show_information_path %>
    </li>
    <li class="profile-tab">
      <%= link_to 'Signature', show_signature_path %>
    </li>
  </ul>

In my routes.rb I wrote:

  map.show_information '/user-information', :controller => 'user', :action => 'show_information'
  map.show_signature '/user-signature', :controller => 'user', :action => 'show_signature'

now my problem:

clicking on my "information" link will redirect me to http://localhost:3000/user-information (cause I told him this path in routes.rb - I think) and I get an error:

uninitialized constant UserController

But that's not what I want... My user show path is something like:

http://localhost:3000/users/2-loginname

(by coding

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

in my user model)

I want to link to somethink like http://localhost:3000/users/2-test/user-information. Any ideas how it will work? Any ideas why I get this error?

解决方案

As far as Rails conventions go, the model itself is singular (User) but the table (users) and controller (UsersController) are both pluralized. This can cause a significant amount of confusion at first, and even after years of working with Rails I still make the mistake of trying things like 'user = Users.first' which is, of course, not valid, as often you get to thinking about table names instead of class names.

Also, for toggling the display of elements on a page, you probably want to use the link_to_remote method which uses AJAX for updates instead of a page refresh. If you're okay with a full page refresh, those actions will need to redirect_to something, such as the page referrer, or you will get a blank page or error since the page template does not exist.

Typically what you do is:

<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>

Then each action is as you have specified, however, the page template show_information.rjs would look like:

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

Keep in mind you will need to have a placeholder to receive the partial contents, so simply wrap your optional sections in an element with a specific 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>

这篇关于Rails:链接到部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆