Render Ajax question - ruby-on-rails

Excuse me, Perhaps it is a foolish question.
there is a way for render a view (with action include) inside other view through ajax request?
I need to use ajax but i need that data don´t charge in principal action. I need that data charge in other action.
I don´t want to use a partial.
Thanks in advice

You can have an empty container div and fill it with the return data of a jQuery.ajax call to an action on one of your controllers with render :viewname, :layout => false

Related

Rails - Same Model/Controller, but different View

I am constructing a form wizard that guides the user through a form that has already been created and deployed. The model and controller should stay the same as the only thing of change is the view (guiding the user through each form field). What is the best (and easiest, if possible) way of accomplishing this task?
Even through the wizard, once the user saves their form, it gets saved to the same database via the same model and controller. From doing a bit research it seems that this is possible by obviously creating a new view, create a simple controller that extends the original controller, and routing the new controller to the new view.
Any suggestions are really appreciated. Thanks!
After doing some research, I figured out a couple of ways to accomplish my task:
A simple way is to create a wizard action within the form's controller. Have the wizard action render the wizard's view.
A better way is to create a wizard controller and have it subclass the form controller. Override the new action, and render the wizard view. This is the more preferred method because basic CRUD actions in Rails acquire REST for free. Thus, following this method will yield a RESTful wizard.
You should definitely look here:
http://railscasts.com/episodes/217-multistep-forms
Otherwise, to answer shortly, you can tell any action of your controller to render ay view you want.That's what is done in the basic scaffold controller:
render :edit

Display AJAX controller action result using HtmlHelpers

I have a fairly complex object which has some C# code written to render it as HTML in various views.
There is also a view which can call an AJAX method of a controller, which returns the complex object serialized to JSON which should then be displayed.
This seems to leave me requriring complicated duplicate code to render the resulting JSON as HTML using Javascript/jQuery.
The obvious solution is to render the HTML in the controller action and return this from the AJAX call. However this seems in violation of the MVC pattern so not really a good option.
Is there a different way I can render the object returned from the AJAX method making use of the existing C# code?
Thanks.
Create a PartialView to which you render the object, and return that.
As mentioned, either create a PartialView user control and return that, which you can inject the HTML in nicely (returns the HTML as a string) or you can use a templating option in JQuery or something else to do the UI generation for you.
HTH.

Calling a controller from another

I want to call an action of a controller from another controller.
How can I do this?
Some years ago, there were compononents, but they are not available any more.
Best regards
You can't call an other controller's action method.
You have only two solutions.
Doing a redirect to the appropriate URL.
redirect_to '/'
But of course if you have datas from a form, you lose them.
Render the action from an other controller.
render 'controller/action'
You keep all your defined datas (params and everything).
But you need to do again all what the other controller would do.
One solution to avoid repeating code lines would be to have a library method defining every vars you need in your view and use this method in your two controllers.
redirect_to is the preferred way of doing this.
If you just want to render the other action's view with the current action's logic you can pass the view as an option to render.
render 'other_controller/action'

Passing object from view to controller

Is there a way to pass a object, specifically a form_builder object used in a view, to an action in the controller? I am using a link_to_remote and want to update a variable in the controller and then add a new nested form element to my form via a render :update block.
If I pass it as a param, it gets converted string and I can't seem to successfully pass a locals hash to the controller either.
No, you cannot pass an object from the view to the controller.
All your logic should happen in the controller and model and at the end of the process the view renders the result in the browser or other places.
You can only use params, whatever you need to send to controller, must be serialized inside params. I'm sure you don't need to send whole form_builder object, send there just what is needed to construct new "subform"
BTW you should construct your views and controllers to work without any ajax, and then progressively enhance your forms and controllers to also support ajax. RJS and render :update is bad practice and should be avoided. find someone who understands javascript instead of writing pseudocode with Rails' RJS

How do I pass a parameter to a form in Rails that persists on validation error?

I'm attempting to create a hierarchal menu system in rails.
My menu model is in a tree data structure, and I need to pass the parent information to the new action. The best way i found to do this was to pass the parent's id as a querystring. This works, unless validation blocks creation. The problem is that after the new view is rendered, the parameter is no longer present in the url.
Am i going the wrong way with this? Should I just be persisting the value in the session? Should i create a dropdown to pick the parent item (I'd prefer to just let the user click "Add new item here"). This must be a common issue, but I havent been able to find antything relevant.
Update: To clarify a question, I tried doing the following to pass the parameter when the view is rendered in the failure case, but the url has already been set at that point (we've already posted to the controller's base url).
format.html { render :action => "new", :parent_id=> params[:parent_id] }
I need another way to pass the parameter.
I figured it out. I just added a hidden field to hold the parent_id. When the browser requests the new form, i put the parent_id into the model object, and it's persisted on psotback.
Not 100% clear on what you are trying to achieve.
After the operation fails validation, you render the view again?
Can you not just pass the parent_id to this view as a parameter?

Resources