Rails 3 link_to 部分结构

发布于 2024-12-10 13:14:43 字数 1342 浏览 1 评论 0原文

我想创建一个 link_to 渲染 :partial。我已经在视图文件夹中存储了两个部分,我在其中调用 link_to (这是配置文件视图)。下一步是让 JavaScript 加载数据,但我在正确构建 link_to 时遇到了困难。

这就是我所拥有的:

<ul id="infoContainer">
  <li><%= link_to render(:partial => "about") do %>About<% end %></li>
  <li><%= link_to render(:partial => "other") do %>Other<% end %></li>
</ul>

使用这些,两个部分都在我的 show.html.erb 中呈现,并且链接消失了。

所以我尝试了以下操作:

<ul id="infoContainer">
  <li><%= link_to render(:partial => 'about'), :class => "a", :remote => true do %>About<% end %></li>
  <li><%= link_to render(:partial => 'other'), :class => "o", :remote => true do %>Other<% end %></li>
</ul>

两个部分仍然显示,并且“关于”/“其他”文本链接仍然不显示。

更新:所以我可能可以使用 link_to 。它只是不渲染任何东西。我想必须用 JavaScript 来解决这个问题。

<li><%= link_to "About", (:partial => 'about'}, {:class => "a"}, :remote => true %></li>
<li><%= link_to "Other", (:partial => 'other'}, {:class => "o"}, :remote => true %></li>

使用上面的 link_to 生成 URL:/profiles/1?partial=about

I want to create a link_to render :partial. I have two partials stored already in the view folder in which I call the link_to (which is the profiles views). Getting the JavaScript to load the data is the next step but I am having trouble structuring the link_to properly.

Here's what I had:

<ul id="infoContainer">
  <li><%= link_to render(:partial => "about") do %>About<% end %></li>
  <li><%= link_to render(:partial => "other") do %>Other<% end %></li>
</ul>

Using these, both partials rendered in my show.html.erb and the links disappeared.

So I tried the following:

<ul id="infoContainer">
  <li><%= link_to render(:partial => 'about'), :class => "a", :remote => true do %>About<% end %></li>
  <li><%= link_to render(:partial => 'other'), :class => "o", :remote => true do %>Other<% end %></li>
</ul>

Both partials still show and the "About"/"Other" text links still don't show.

UPDATE: So I may have the link_to working. It's just not rendering anything. Gotta fix that with JavaScript I assume.

<li><%= link_to "About", (:partial => 'about'}, {:class => "a"}, :remote => true %></li>
<li><%= link_to "Other", (:partial => 'other'}, {:class => "o"}, :remote => true %></li>

Using the link_to above makes the URL: /profiles/1?partial=about

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-17 13:14:43

您错误地使用了 link_to - 请检查 API / 文档,

它被称为:

link_to(body, url, html_options = {})
  # url is a String; you can use URL helpers like
  # posts_path

link_to(body, url_options = {}, html_options = {})
  # url_options, except :confirm or :method,
  # is passed to url_for

link_to(options = {}, html_options = {}) do
  # name
end

link_to(url, html_options = {}) do
  # name
end

请参阅:

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

或许:

rails3 (link_to_remote) 的 link_to 语法和基本 javascript 不起作用在 Rails3 应用程序中?

you're using link_to incorrectly -- please check the API / docs

it's called either:

link_to(body, url, html_options = {})
  # url is a String; you can use URL helpers like
  # posts_path

link_to(body, url_options = {}, html_options = {})
  # url_options, except :confirm or :method,
  # is passed to url_for

link_to(options = {}, html_options = {}) do
  # name
end

link_to(url, html_options = {}) do
  # name
end

See:

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

and perhaps:

link_to syntax with rails3 (link_to_remote) and basic javascript not working in a rails3 app?

追风人 2024-12-17 13:14:43
<ul id="infoContainer">
  <li><%= link_to render(:partial => 'about'), urlyouwant %></li>
  <li><%= link_to render(:partial => 'other'), urlyouwant %></li>
</ul>

如果您想在用户点击时运行 javascript,您可以在 http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-link_to_function

<ul id="infoContainer">
  <li><%= link_to_function render(:partial => 'about'), "alert('About!')" %></li>
  <li><%= link_to_function render(:partial => 'other'), "alert('Other!')" %></li>
</ul>
<ul id="infoContainer">
  <li><%= link_to render(:partial => 'about'), urlyouwant %></li>
  <li><%= link_to render(:partial => 'other'), urlyouwant %></li>
</ul>

If you want to run javascript when user click on, you can read more at http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-link_to_function

<ul id="infoContainer">
  <li><%= link_to_function render(:partial => 'about'), "alert('About!')" %></li>
  <li><%= link_to_function render(:partial => 'other'), "alert('Other!')" %></li>
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文