Render another controllers view as partial (Rails 3) - ruby-on-rails

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.

Related

Generic application controller action

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.

Ruby on Rails - Populate modal with data from database?

Pretty stumped at the moment trying to figure something out. I have a modal that shows a table, and for each of the entries in the table, I have actions (i.e., show, edit, delete, etc). When the user clicks on "Edit" for an entry row in this model, I want it to populate another model with a form to show the data associated with that entry.
Is there a convenient way to do this? For one, I do not know how to pass a parameter to a modal from another modal. I don't know how to make this form "reinitialize" after it's already been rendered when the first page is loaded.
Any suggestions?
I have been in the same situation and I suggest that you just keep the one modal and replace the contents through AJAX (IMO). Once you have rendered into the modal in the first place, your links can then just render as AJAX the same way that they would and overwrite the information inside the modal. The way I did it was this:
Populate the modal (you've already done that).
Create a link = link_to "Text", "url", remote: true (let's say this is the edit action).
Run the edit action in the controller as usual.
The edit.js.erb file would contain one line: $('#Modal_content').html("<%= j render 'edit' %>") (#Modal_content is just a div that I put in my modal so that I can replace all the contents without messing up the close button and other modal required html).
The _edit.html.erb file is called where you would put all of what you needed that comes from the edit action.
As for passing information, the id is passed through the link that you click on to call the controller#edit action.
Let me know if you need more details, but that should get you most of the way there.

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.

Updating part of a web page on Rails

I have a Rails controller with a form, and I want that when I post this form, a table on this page is updated via AJAX. I know a way, using partials to achieve this, but is that any way to do this without partials? And without putting code for my view inside my controller too.
Thanks
You can make a .js.erb file as the view, and from the form call the action from link_to_remote. That will translate into an ajax call to the action, that will then execute the js from the view. Inside that js.erb file you can do whatever you like. Although it will be hard to render part of the table server side if the code isn't broken out into a partial.

Resources