tvalent2
tvalent2

Reputation: 5009

Rails 3 link_to partial structure

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

Upvotes: 0

Views: 4024

Answers (2)

KingOfDCP
KingOfDCP

Reputation: 13

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

Upvotes: 0

Tilo
Tilo

Reputation: 33732

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?

Upvotes: 3

Related Questions