Rails: Create a form wrapper using partials - ruby-on-rails

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

Related

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?

Where should I put XML Builder code?

I am creating a method to generate an XML document via Ruby Builder.
Where should I put the method that created the XML markup? Should it be a method on the model?
I plan to have the XML document pull from multiple models via associations, so I think I need to have it in the controller or a helper, but I would like some in put on the best place.
If it's practical for you, I'd put it in your views folder. This lets you follow the traditional pattern of "load stuff in the controller; render stuff in the view," and has the extra perk of keeping what's probably a messy and very specific method in its own file and out of the way.
Now, I'm not sure if there's a Ruby Builder template format, but you could always just wrap your code in <%= ... %> and treat it like a regular ERB file - should work about the same.
Hope that helps!

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

Is it a good idea to create a helper method for this type of scenario?

I have this code in my html.erb at many places.
<div id="left-nav">
<%= render :partial => 'tests/tests_left_menu' %>
</div>
Is it a good idea to create helper method for this type of code ?
How to write this code in helper ?
I see a few good strategies to use in your situation. Pick and choose based on your project's specific requirements.
You can just put div#left-nav and its contents into yet another partial like tests/tests_left_menu_with_wrapper. This saves you a couple of lines.
If you can generalize the cases when the entire segment appears, you can move it into a layout. This way, once you declare the layout for a particular action using the ActionController::Base.layout method, you'll be able to skip writing the entire segment altogether.
You can write a helper, but it's not clear what advantage it confers over simply using content_tag. You're probably better off using partials or layouts.
Personally i don't think there's a need to, and i think it's more like because you are not using other tools like haml to help reduce the number of lines in an erb files
the same code can be achieved in haml in just 1 line:
#left-nav= render :partial => 'tests/tests_left_menu'
hope this helps =)
I suppose if you have that code in many places I'd move the the div into the partial. If you need the flexibility to have tests_left_menu outside of the div I'd still pick two partials over a helper in this scenario. Avoid writing html in Ruby when you can :)

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