Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Load partial from link_to in rails

rails

  • Please log in to reply
2 replies to this topic

#1 sidonrailsforum

sidonrailsforum

    Passenger

  • Members
  • 1 posts

Posted 19 November 2013 - 10:41 AM

Hello , 

 

 <%= link_to 'Add Tester', :onclick => "render :partial => 'somepartial' " %>    , But it fails to render partial while clicking on add tester . 

 

Any suggestions ?



#2 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 19 November 2013 - 06:00 PM

Hmmm! that just "feels" wrong.

Perhaps javascript is disabled?

 

You should just link to an action in a controller and arrange for the action to display the partial

 

For javascript functionality you should use unobtrusive javascript. Attach the link to a javascript function in whatever_copntroller.js

A great cast on how suimple this is to do can be found here http://railscasts.co.../390-turbolinks

 

It is a cast on turbolinks but is equally appropriate whether or not you are using turblnks. It may even solve your problem as your issue could be turbolinks related but you don't say what version of rails you are using.

 

Anyway, follow the approach taken on that railscast plus a normal link_to controller action and your links will work whether or not javascript is enabled.


Programming is just about problem solving!


#3 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 19 November 2013 - 07:33 PM

You can't render a partial in that way. What you'd want to do is having the controller action render the partial and injecting it via some kind of javascript.

 

E.g. 

# view
<%= link_to "Add tester", add_tester_path, remote: true %>
<div id="tester"></div>

# controller
def add
  respond_to do |format|
    format.js { render partial: 'somepartial' }
  end
end

# _somepartial.js.erb
$("#tester").html("New content");

Blog: http://ohm.sh | Twitter: madsohm






Also tagged with one or more of these keywords: rails

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users