PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 18, 2022

[FIXED] How to use one partial in different contexts

 May 18, 2022     partial, ruby, ruby-on-rails, view, yield     No comments   

Issue

i am new to rails and try to do the following:

I would like, because i use Bootstrap, to have a partial for a input field, with it's label and a little icon i called symbol in this case.

Here is my view:

<%= form_for(@user, :class => "form-horizontal" ) do |f| %>
 <fieldset>
  <%= render 'shared/text_field', function: f, tag: :name, symbol: '<i class="icon-user"></i>' %> 

  <%= render 'shared/text_field', function: f, tag: :email, symbol: "@" %> 

  <%= render 'shared/password_field', function: f, tag: :password, symbol: '<i class="icon-log"></i>' %> 

  <%= render 'shared/password_field', function: f, tag: :password_confirmation, alt: "Passwort wiederholen", symbol: '<i class="icon-log"></i>' %>
  <!-- SUBMIT -->
  <%= f.submit "Anmeldung", :class => "btn btn-primary" %>
 </fieldset>
<% end %>


Here a subpartial for normal input fields:

<%= render 'shared/bootstrap/input_field' %>
<% content_for :label do %>
   <%= function.label tag, :class => "control-label", :for => "prependedInput" %>
<% end %>
<%content_for :symbol do %>
   <%= raw(symbol) %>
<% end %>
<% content_for :field do %>
   <%= function.text_field tag, :class => "input-xlarge", :id => "prependedInput", :size => "6" %>
<% end %>

(there is also a subpartial for password fields, basicly exchanging function.text_field with function.input_field)
And here is the input_field which is rendered:

<div class="control-group">
  <%= yield :label %>
  <div class="controls">
    <div class="input-prepend">
      <span class="add-on"><%= yield :symbol %> </span>
  <%= yield :field %>
    </div>
  </div>
</div>

So my question is: how can i solve this Problem in a nice and easy way (this things happend while refactoring and it got even worse then better) and how can i make it work, because by now something like this happens: http://pastebin.com/bNsgT9AR so yield puts with each content_for the content and the content before into the place (except the last one)

It would be great to hear nice solutions from you, there has to be a so much nicer way as almost always in Rails.

Greetings form Germany ;)


Solution

As the author of The Rails View, I agree with Ben Taitelbaum's post on this and say that Formtastic is totally a great option for this kind of complexity.

With regards to the code you posted, as developers we want to avoid that kind of view writing across the board, as it ends up an unmanageable mess. There's too many files involved in editing a form, and at some point, some other dev will come in and overwrite our shared partial, not knowing where else it was used, to change the way a different form behaves. Any benefit of code reuse that we can get out of this kind of abstraction is completely obliterated by the potential for it to go sour very quickly with mutliple developers.

While Formtastic is great, I've also been using SimpleForm a lot in my recent apps, especially when I don't need the full power of Formtastic.

SimpleForm will handle your label, and it also supports i18n as well. With this it would be:

<%= simple_form_for @user do |f| %>
  <%= f.input :name, :input_html => { :class => 'user-icon' } %>
  <%= f.input :email %>
  <%= f.input :password, :as => :password, :input_html => { :class => 'log-icon' } %>
  <%= f.input :password_confirmation, :as => :password, :input_html => { :class => 'log-icon' } %>
  <%= f.button :submit %>
<% end %>

Then in your I18n file, you can have:

en:
  simple_form:
    labels:
      user:
        username: 'User name'
        password: 'Password'
        password_confirmation: 'Password confirmation'
de:
  simple_form:
    labels:
      user:
        username: 'Benutzername'
        password: 'Passwort'
        password_confirmation: 'Passwort wiederholen'

And more as you need it. You can define your hints and placeholders in the i18n file as well.

Also, think about adding the icon as a CSS pseudo class :after, and do so for each class that you need to add in the code)

input.user-icon:after {
  // image as background, positioned properly, etc etc
}

input.log-icon:after {
  // image as background, positioned properly, etc etc
}

And that way, you can remove this kind of stuff from your ERB/HTML and put it where it belongs: In the CSS (presentation layer).



Answered By - John Athayde
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing