Rails 3: How to render an action from another controller's view - ruby-on-rails

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

Related

How Do I Submit Information from Multiple Partials?

I have a page with a form on it. I dynamically generate the form based on information i have available to me in the database, but it works out to look a little like this:
MainView
-Partial1
-Partial2
--Partial2a
--Partial2b
All of the partials contain parts of the form. I've placed breakpoints in the code, that show me on the controller (in this case "Create"), the object does not have any of the information from the form populated in it.
This is the code i'm using for the 'create' button:
<button asp-action="Create" asp-controller="LSAA">Create</button>
How do I make sure that my post includes the information from the MainView, as well as the information from all the partials rendered as part of that main view?
The main view and the partials all render as one view from a browser, and call resources (js, css.. etc) from the perspective of the main view. If anything goes wrong - it means the configuration of the partials into the main view is probably the issue. When you say you don't get information from the partials do you mean it's not posted to the database? Or do you mean form data from that partial specifically does not appear in the view?

Rendering a partialview from within a helperclass in Umbraco

I have some html that is used a lot in the site i'm building. So I created a App_Code\Helpers.cshtml file and placed the helperfunction in that file.
Now, I want to render a partial-view (a MVC view for a form). But I can't use #Html.Partial("MyFormPartial", new formModel())
I can't find any other ways of rendering a partial view from within a helper class. Anybody got an idea on how to solve this?
Is a seperate helpers.cshtml even the best way for this kind of repeating html-code? I think it gives me a bit more freedom in the parameters I'm providing, instead of the macro's. But it sucks I can't use #Umbraco (without creating your own helper) or #Html :(
Just pass #Html to the helper function if you don't want to create it inside the helper function.
Nevertheless, isn't it a better idea to use a child action and render part of code you'd like to been shared?

Rails: Create a form wrapper using partials

I have a form_for in all of my views. Now I need to insert a hidden html input element in all of these views.
Is there anyway I could specify a wrapper over form using partials where I take all the form parameters , do my own logic of inserting the hidden input element and render the rest of the block given by the views? How can I do this?
Yes, I think you can write your own logic in views through helpers, Helpers provide you more flexibility, consistency, and also provide functionality to keep your code DRY(the basic feature of Rails). you can write your helper methods in app/helpers/application_helper.rb.
You can have a look on this guide:
http://guides.rubyonrails.org/form_helpers.html
Hope it will help you in what you were looking for.Thanks

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: Creating an HTML Snippet with a Variable?

I have a submit button that is a block of HTML code because of styling and images to make it look better. (I stole most of it from Wufoo).
This is one every form in the application and I was wondering if there is a cleaner way to do this. Something like a partial or helper?
The name of the button "Submit" or "Add Contact" needs to be a variable.
snippet
Add Contact #variable text
Back
* Required
You can use a application-wide helper for this. Helpers are modules containing methods that are shared by multiple views. You can easily define your own helper that works like the 'submit_tag' helper method that generates the button.
consider partials (http://api.rubyonrails.org/classes/ActionView/Partials.html)
Seems like something partials were invented for.

Resources