Rails: Creating an HTML Snippet with a Variable? - ruby-on-rails

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.

Related

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

Where to put reusable HTML code in rails?

I'm writing a rails app that allows users to delete records of various sorts. After pressing a delete button, I'd like to show a confirm dialog using bootstrap. I'd like to use this same dialog in several of my views, so I'll need to include the same HTML snippet in most of my pages.
I'm new to rails and I'm still learning the conventions. Can anyone suggest the best (or standard) place to put the dialog code? Should it be a partial in views/layouts/_confirm_delete_dialog.html.erb, should it go inside application.html.erb, or should I put it somewhere else?
Thanks in advance for your advice,
D.
Within your Views folder, you can create a general folder (called whatever you want). If you have a variable that you need to pass through to the general layout, you can definitely do so, but you will want to make sure that the information that you're passing through doesn't cause conflicts with the fields pulled from the model. For example, if you have two models and one has a public field but the other doesn't then you will not want to have a generic message that is using the public field. However, something like the created_at or updated_at would be okay.
You would use a code similar to,
<%= render 'general/simple_message', :f => f %>
In your views folder, you would have a directory called general and a file called _simple_message.html.erb.

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 :)

Resources