Generic application controller action - ruby-on-rails

I have a rails app that displays user activity on the RHS on each page.
Presently I pass the collection to the partial directly:
<%= render partial: "activities/activity", collection: current_user.activities.order(created_at: :desc) %>
I wish to now paginate this activity list.
current_user.activities.order(created_at: :desc).page(params[:page]).per_page(10)
I am guessing I need to have this set as an instance variable and have it placed in a route that can be accessed from the view.
My question is where should I define this instance variable as the route needs to be generic as the activity is displayed on a views.
If it helps I am doing the pagination with ajax, "remote: true".

I think what you need to do is have a controller action that receives the ajax request which would include the page number. This action would pull up the relevant activities for that page (using the code you put in the question) then it would render some js which would clear the activities panel and repopulate with the new page of actions. It would also have to re-render the pagination controls so that the links are updated with the new page numbers.
If you're using a gem for pagination then it probably has a method to call to generate the pagination controls, and those controls will link to the route to the controller action that I described, passing the relevant page number as a parameter.

Related

How to call multiple controller actions into the single page tabs in ruby on rails

I have three models, controllers and views.
1. package
2. source
3. measure
Everything has separate forms. I created a controller and view called scenario and created three tabs(Package, Source and Measure) in the scenario page.
I need to render the three tabs to call each one of the above controller and views like Package should do the action of create, edit, update and destroy of Packages controller and vice-verse.
How should i call those three different controller actions in the Scenario page? I want all the three into the single page and work within their tabs.
What shall i include in the index, new, create, edit and update methods in Scenario controller? Do i need to create all the view files for scenario too?
Any help would be more appreciated.
P.S. I tested all the three are working fine with their separate pages.
best to use ajax calls and render the view templates within the respective tab with js/erb template. e.g.,
show.js.erb
$('<%= #selector %>').html("<%= escape_javascript(render(:action => :show)) %>");
if you wanna use this same ajax call to render show action elsewhere, then send a selector as a request param depending on which page you are on: e.g., when you are on the scenario page and the div id for package tab is say 'package-tab', then send selector '#package-tab' as param and set #selector to this in your controller. with this you can control from the request which dom-element is refreshed by the ajax call.
does this answer the question?

Jquery redirect with Ajax Calls to another page Rails

I am trying to implement two pages. The first page being a selection of the items I want to show on the second page.
On my first page, I have done a selection of the items which I want to render.
On click of a button, 1. the ids of these items will be placed in an array and 2. the user will be redirected to a new page through:
window.location.replace("/schedule");
Right now, I want to achieve the following:
Do an ajax call through rails to get each of the array items. i.e. make an ajax calls to the urls of myclass/[id]. The id belonging to the items in the array.
I am not sure how I could get my array items from the current page to the next page or if there is a better way of achieving this. Any advice or suggestion will be greatly appreciated.
Add :remote => true to your form. Then, app will render your action_name.js.erb view instead of action_name.html.erb. In js file you can put code, which will repleace div without redirection, f.e.
$('#div_id').html('<%= escape_javascript render(your_partial_name) %>');
You don't need to do anything special in partial and controller action, its all the same.

Rails - Reload action with new value in params

I have the following relation in my model:
A user can have several consumers
According to that, I call several actions from different controllers that depend on that consumer_id in the URL. So, I do stuff like:
/:consumer_id/products/all
/:consumer_id/locals/all?params...
I want to be able to given that I am in a particular view, let's say /3/products/all, be able to refresh, redirect or whatever is the best to /4/products/all, with a form that shows the different consumers attached to that user.
I know how to display the form, but I fail to see which action should I put given that I want to load the current controller, current action and the current params, except that I want to change the params[:consumer_id] and the session[:consumer_id] to the one chosen by the user in the form of the view.
What's the best or appropriate way of doing so?
This is a link to the same page with the same parameters and one additional parameter:
<%= link_to "Show more results", url_for(params.merge(:per => 100)) %>
If you want to modify a parameter rather than add a new one, you can modify the params hash just as you would modify any hash, and use url_for again.
Inside the view you can access both the current controller using controller_name and the current action using action_name
Your question is not clear. You want to choose consumer_id from your form, then use that consumer id inside the same page or for links to other pages?
If you want to use the consumer id for the current page, you need to use javascript or JQuery to update all attributes of the current page.
If you want to use that consumer id for links,
you may also use JQuery to update all links on your page
Or, you can submit the form to the server with new consumer_id.

Render another controllers view as partial (Rails 3)

Sorry, only have been using rails for 10 day and I'm completely lost. Have a post model/controller and comment model/controller.
In post/show, I want to click an ajax button and replace a div (#addcomment) with the partial generated from comment/new (The new action takes two params and builds a comment for an ajax form partial). The comment/new partial is an add comment form with remote => true.
Anyone willing to point me in the right direction?
Tried using an ajax button in the view, that calls an action that replaces the div with the comment/new but it didn't work.

How can I re-populate a list in a <div> after adding an item to it using AJAX?

Specifically, I have a number of pages in my Rails app that use the same partial. In the action handler for each page I create an array object (e.g. #list_elements) based on a database query. Each page uses a different query so that each page has different list elements in it. At the top of each page I have a form_remote_tag containing an edit field, allowing the user to add a new element in a dynamic, AJAXy fashion (think something like Twitter 'What's happening' box).
My problem is that when the AJAX command fires I need to reload the list to include the newly added item, but the contents of the list were determined by a database query. I need to remember which query applies to the current page (i.e. controller action) so that I can run it again. I thought about storing something in the rails session structure but it seems like overkill - it's like storing the current page all the time.
Anybody done anything like this and have a nice Railsy way to achieve it?
Ben
Couldn't you just re-render the partial in your rjs template?
page[:div_element].replace_html :partial => 'partial'
If you perform the query and define the array in the controller action, then an ajax call will refresh that array.

Resources