-1

Half of the current project that I am working on is written in angularjs while most critical parts are written in Rails.

Is it possible to use angular without rewriting all the rails pages ?

At the end of the day, Rails is already handling all business logic in backend. All I want is a neat way of using angular to render that data without reloading page to fetch the templates rendered by Rails.

So it is possible to get partial of a page by changing my application.html.erb to just one line of code:

<%= yield %>

Now, If I reload the url rails only returns the part of page relevant to the specific business logic from controller that should handle the request.

No I need to do is - Use angular to render the footer and header for these pages - Handle a page reload logic, so that on a full page reload, it should load the single page app and then render the current view.

  • you could retreive partial from rails and wrap it under a route in ui.router – Nishant Feb 23 '18 at 07:38
  • 1
    Will that mean rewriting all Rails pages to return without header and footer ? –  Feb 23 '18 at 07:50
  • You might not need to do that, check for your appliction.erb.html there you can define a logic to conditionally return header and footer. – Nishant Feb 23 '18 at 07:51
  • You could also have your rails api return a string respresentation of a certain partial with `render_to_string partial: '...', locals: { some_date: [] }` and then use that in your angularjs app. https://stackoverflow.com/questions/20726007/how-do-i-render-a-partial-to-a-string – Eyeslandic Feb 23 '18 at 09:36
  • 1
    and then I can make angular compile string into view, makes sense –  Feb 23 '18 at 13:15
  • Read the post here : https://medium.com/@nshnt/migrating-rails-pages-to-angularjs-6c01ced6cb88 – Nishant Feb 23 '18 at 13:18
  • 1
    this has been put on hold ? @Rob not sure why would you assume it lacks research. –  Feb 24 '18 at 10:38

1 Answers1

0

Based on the blog : https://medium.com/@nshnt/migrating-rails-pages-to-angularjs-6c01ced6cb88, here is what seems to work

If request has a partial=true query param, server returns the partial, without header and footer.

helper_method  :should_render_partial?, .... 

def should_render_partial?
    params[:partial].eql?("true")
end

In the template :

<% unless should_render_partial? %>
    <%= render partial: 'layouts/angular_app_template' %>
<% end %>

<% if should_render_partial? %>
    <%= yield %>
<% end %>

In angular, using ui.router the same partial can be rendered :

$stateProvider.state('home', {
  url: '/home',
  templateUrl: function(){
    return "/home?partial=true";
  },
});