Updating part of a web page on Rails - ruby-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.

Related

What is the Rails Way (in 3.2.6) to respond to JSON with controller actions?

I have a simple form in a Rails 3.2.6 application that has a select box. Based on the content in the select box on the change event, I'm submitting an Ajax request.
Then I have a .js.erb file that is rendered. But then I'm using that .js.erb file to render a partial.
Is that the Rails Way, or should I just skip the .js.erb file and render the partial from the controller action? It seems like an unnecessary step.
If you don't have anything else in the js.erb file then I'd render the partial from the controller.
Personally I don't like generating / returning partials directly from controller and I would strongly suggest calling partial from appropriate .js.erb file - this way you will always know, what is returned after calling specific action, without looking at your controller.
But I don't know, if this is a Rails way, it's just my common practice.

Displaying flash message in portion of page that is otherwise not updated

I'm looking to display my flash messages in a portion of the page that is otherwise not always in a partial that gets updated.
In other words, I may submit a form that updates a partial via ajax. But I want to display the flash message in a portion of the page that is outside of that partial.
I could have some javascript in every single necessary js.erb file to update the flash partial, but that seems crazy. Is there a more simple way of going about this?
I don't have to necessarily use flash messages either if something custom would work better.
Thanks!
You can do it the low-tech way by using a :remote call on your form that, when executed, will inject some HTML back into your page from a partial of your choosing.
It's pretty easy to do in a .rjs view:
page['flash'].html(render(:partial => 'flash'))
You can also do it in a .js.erb view using jQuery:
$('#flash').html("<%= escape_javascript(render(:partial => 'flash')) %>");
I tend to think the .js.erb method is a lot more ugly, but we all have our preferences.

Perodic AJAX calls

I have a page where I want to do an AJAX call and possibly render a partial every 30 seconds or so. Now, I know how to do this with jquery AJAX, doing an AJAX call and passing in the path, but it doesn't seem like the Rails way.
I say this because the result is that the main view does not show you the entire structure of the page. When you render a partial, you at least see the partial's positioning inside the document, if you render a partial via AJAX, you have to read the javascript code to know that it's there.
Is there a more unobtrusive (for lack of a better word) way to do this?
When I need to do something like this, and want the partial block to be positioned, I include a container div that I then render the AJAX call into. That was I can position the partial prior to it actually being rendered.

Rails 3: How to render an action from another controller's view

I'm trying to call an action from a different controller's view:
Controller Countries has an action called selectbox which generates a html selectbox from all countries as a partial.
Controller Customers has an action called new which generates a html form for all the customers attributes. I want to insert the selectbox from Countries#selectbox in this form.
What would be the right way to achieve this?
You're doing it wrong. If there's a piece of code that is to be reused (such as html selectbox generation), you should put it in a helper and/or use a partial to render the html selectbox part.
Bear in mind this is only good advice if the code is somewhat complex (say, more than one or two lines). Here's a post I found while googling that may help you: helper or partial
For what you are doing, extracting the code into helper method is the right way to do it. However, if you still want to use am action from another, this is the official Rails plugin you can use:
https://github.com/rails/render_component

Can I have form tag inside View in MVC?

Can we put form tag inside view in MVC. I need to keep report controller inside the form tag which is inside the view. It works fine. I am new to MVC, can anybody tell is it the correct way to work with.
Yes, you can put form tags in views. Forms are standard html tags, so it's not a problem. You can also put form tags with runat="server" if you want to render some server controls that require it.

Resources