jrocc
jrocc

Reputation: 1346

Render partial on button click

I'm having trouble rendering a particle on a button click. I'm getting the error

HelpController#FAQ_help is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []

main_page.html.erb

<%= link_to "FAQ", FAQ_help_path  %>

<div id="content">

</div>

help_controller.rb

def FAQ_help
  respond_to do |format|
    format.html {}
    format.js
  end
end

FAQ_help.js.erb

$('#content').html("<%= render :partial => 'FAQ_help' %>");

_FAQ_help.html.erb

<div>
  <h1> This is the FAQ </h1>
</div>

routes.rb

get 'FAQ_help', to: 'help#FAQ_help'

Upvotes: 1

Views: 7394

Answers (2)

rony36
rony36

Reputation: 3339

How about this:

# main_page.html.erb
<%= link_to "FAQ", FAQ_help_path, remote: true %>

And

# faq_help.js.erb
$('#content').html("<%= escape_javascript(render :partial => 'FAQ_help')%>")
#fixed syntax

Cheers!

Upvotes: 4

user1898553
user1898553

Reputation: 101

Since you have created a method for this request I would define it a little more for clarity:

def FAQ_help
  respond_to do |format|
    format.html {}
    format.js {render: FAQ_help}
  end
end

If you want to keep your controllers clean I would pass a param with the link and check if the pram is present if it is render FAQ partial:

View:

<link_to "Your FAQ"  your_path, data: {method: :get, :remote => true, params: { faq: "#{current.user_name}" }.to_param }, :class =>"button" %>

Controller:

if params[:faq].present?
    respond_to do |format|
    format.html {}
    format.js {render: FAQ_help}
  end
else
  respond_to do |format|
    format.html {}
    format.js {} #your default view for this method 
  end
end

This is handy to display IE:different content as per the request

ie: parms =bob # displays bobs FAQ
ie: parms =ted # displays teds FAQ

This technique is similar to what you would implement to create a search.

Upvotes: 0

Related Questions