rails render view onclick with javascript

Mischa

I'm trying to create a view, where users can click on different buttons and render different content based on the buttons they click. I've tried getting this done with JS but can't really get it to work. I made a button in my view:

<div class="link">
  <%= link_to "Greetings", "#" %>
</div>
<div id="show"></div>

then in job.js.erb:

$(function() {
  $('.link').click(function() {
    $('#show').append("<%=escape_javascript render(:partial => 'show' %>");
  });
});

unfortunately render is not supported in assets, but I don't really know what the best way is to make this happen.

x6iae

One way you can try is to let the button click go to a controller action, by AJAX, and then render the file with name <action_name>.js.erb. this file will then be able to call the render action.

I will expatiate more with the following:

Assuming the resource in question is Greetings and you have a dynamic_show action in the Greetings controller, for example, and you have a dynamic_show_greetings_path routing to this action.

From inside your view, you can have:

<div class="link">
  <%= link_to "Greetings", dynamic_show_greetings_path, remote: true %>
</div>
<div id="show"></div>

and the Greetings#dynamic_show action will be like follow:

def dynamic_show
  respond_to do |format|
    format.js
  end
end

then, in your view directory, you have a dynamic_show.js.erb file, which will contain the script to append the dynamic view as follow:

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

And that solves it for you!

Of course, to now make it dynamic, you have to then pass in params to the controller, and then render content based on the response gotten.

PS:

  • setting remote: true on the link ensures that the call will be an AJAX call.
  • controller actions by default renders the file with same name as the action name, therefore, dynamic_show responding to js will render dynamic_show.js.erb

Hope this throws a great light into it for you... ;)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

rails render view onclick with javascript

From Dev

How to render view in rails

From Dev

Rails - Render asset javascript

From Dev

Render a Rails view in a Bootstrap modal

From Dev

Render an action view into a file in Rails

From Dev

Partial render view fie Rails

From Dev

Rails Render View If File Exists

From Dev

render html from javascript rails

From Dev

Rails - render JavaScript with AJAX or redirect

From Dev

Rails - render JavaScript with AJAX or redirect

From Dev

Rails instance variable not passing to view with "Render"

From Dev

Rails: Render view from outside controller

From Dev

Render a partial view via ajax in Ruby on Rails

From Dev

render json not showing up in view on rails 4

From Dev

Rails Won't Render My View

From Dev

Render a view in rails via AJAX and controller

From Dev

Rails 5.2 doesn't render new view

From Dev

Rails - Render/Refresh the current view with new data

From Dev

Rails view javascript not loading

From Dev

Call javascript function onclick in Rails

From Dev

rails controller action AND javascript onclick

From Dev

How to render a view inside of another view (rails 4)

From Dev

Javascript + Rails View rendering confusion

From Dev

Loading JavaScript in view in Ruby on Rails

From Dev

Rails: Render view content in post-processor (model / helper issues)

From Dev

Ruby on Rails: How to Render Partial in a view via Jquery

From Dev

Rails 4 How to use render layout option in view?

From Dev

Activerecord rails merging multiple select results and render rows in view

From Dev

How to render partial view of one controller to another controller in ruby on rails

Related Related

HotTag

Archive