views:

54

answers:

2

i want to render a partial within a view. so that when button MORE is clicked everything stays the same just additional characters are shown. in my case the whole article.

<%= @article1.content[0..300] + "..." %> 
<%= link_to "more", ....... %>

i dont know what the right methot would be. somehow i have to explain to rails that when button more is clicked it shows me the whole article. maybe i shouldn't use method link_to ..

thank you in advance for your replys

+2  A: 

try link_to_function, use truncate for part and insert hidden tag with full text, switch them using javascript in link_to_function

tig
+2  A: 

What you're looking for is link_to_remote or link_to_function.

link_to_remote will be fetching the rest of the article from your controller and replacing/appending to a DOM element with a partial via RJS. This allows you to minimize unnecessary data being sent, and facilitates handling users that have javascript disabled.

With link_to_function, the entire article will be served when the page is loaded, but the everything beyond the first 300 characters will be hidden by CSS. This is easier to set up but sends a lot more data, it also relies on the user having javascript enabled.

Without looking at the source the average user probably couldn't distinguish between the two methods.

Which choice you go with is up to you. Sorry, I haven't got time to provide code examples, but the internet is full of them.

EmFi
link_to_remote also relies on javascript ;)so the main difference is speed — with link_to_function it will be less than with link_to_remote
tig
This is true, but suppling the url options given to link_to_remote again as the href value in html option allows the link to work even if javascript is disabled.. As in link_to_remote "More", article_path(@article), :href => article_path(@article) Then to complete the disabled javascript failsafe, your controller should discriminate request type (js vs html) to either load the partial or the entire page. link_to_function is definitely faster once the page loads but requires even more work to avoid leaving a dead link when Javascript is disabled.
EmFi