AJAX calls action for a partial? - ruby-on-rails

I have a page show.html.erb with a corresponding action 'show'. Lower on the page, a partial is loaded called _filter.html.erb which contains undefined instances (so basically, on the initial load, nothing is displayed there).
In the show.html.erb, I have a form that uses AJAX and submits to an action called "filter" which is supposed to create some instances for use within the _filter partial (so now, the _filter partial within show will have some content).
But when i submit the form, it says Template is missing Missing template results/filter.
But what it should be doing is staying on the same current template (show.html.erb) and just run the filter action and update the partial _fitler.
Any suggestions?
UPDATE
Following some things i found online, i made a file _filter.js.erb withing the views/results directory (same dir as show.html.erb and _filter.html.erb):
$("filter").update("<%= escape_javascript(render("filter"))%>");
But it doesnt seem to be doing anything..

By rails convention if you use the name _filter then you should put this file in shared folder.
So it's should be shared/_filter.html.erb

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.

Rails 5 after submitting form / action stay on exactly the same spot on page

I have this webshop, and on one page you see
products;
with a submitting form for a booking;
your order with its bookings;
with a removing link for a booking;
and an updating form for a booking.
Both the order.bookings and the products make potentially long lists on a html page.
The whole booking works by only a booking_controller.
What the booking_controller does:
Takings in the (new) params of a single booking or the destroy action.
Saves the new order.
Redirects to the store.
Works fine, just using ruby and html.erb.
Only problem, and this really needs to change, is that obviously after each redirect the browser goes to the top of the page. The browser isn't focussed. Or better to say, the browser should remain, but doesn't.
I get that your doing all these things on the server-side, so a page reload, or better to say, data-refresh, is necessary. What I don't want is building this whole thing again on the client-side (JS).
Can't I simply say something like: after data refresh redirect to exact same spot on page. Ignoring all the difficulties an asynchronous request would give, or am I exaggerating and is a little JS easy?
With Rails, using ajax is very easy however if you're not familiar with ajax at all it can be a bit daunting at first. Luckily there are many tutorials on the subject. Basically, add the remote: true option to your form_for statement and rails will automatically understand you want it to make a 'POST' request in JS format. It's important to realize that the incoming HTTP request is in JS format because you'll then need to specify handling that event in the controller. Such as:
def create
#Do whatever saving and processing you need here
respond_to do |format|
format.html { redirect_to some_path_here }
format.js { } #By not adding anything in the brackets here, you're telling rails to fetch a js view file that follows standard rails convention and so it should be named 'create.js.erb'
end
Then in your controller's views folder create the view file create.js.erb. In that file you'll want to refresh whatever part of the page needs updating which usually involves hiding the old div that was there and appending a partial in its place. I usually leave an empty div with an ID on the page (in this case your new_whatever_page.html.erb that I then call in the js.erb file to append a partial to:
In your create.js.erb add:
$("#the-id-of-the-div-in-your-new-view-page").html("<%= escape_javascript(render 'order_table') %> #This basically says, find the div on the current page with a matching id and then append the partial named _order_table.html.erb to it.
Now just make a partial named
_order_table.html.erb
in the same views folder and put whatever content you want to insert or update.

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.

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.

rails render question

My question is about rails rendering rule. if the the following line of code
render "intentions"
appears in a rails app, how do you interpret it? my understanding is that rails will try to find an action named intentions, and find the action's template file named intentions.html.erb
under the current controller's view directory, and finally render it.
but in my caee, in a
100% working app, there is no action named intentions under the current controller. but there is a template file named _intentations.html.erb under current controller's view directory and
it is this template finally be rendered.
what kind of rendering rule it is? I need a explanation. thanks in advance.
Rails automatically looks through your views when rendering to see if any match. If you are calling render from within a view it looks for a partial view, which is defined by a underscore as the first character in its name.

Resources