formatting a form inside my rails app - ruby-on-rails

I have a rails app with form inside of it. my css file is all ready. i just don't know how to apply the css form classes and ids also to the fields inside of my create action page where
my form_for lives?
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</p>
<p class="button"><%= f.submit %></p>
<% end %>
my css styles for the form tag and for its fiels as below:
id="phx-signup-form"
e-mail field css style:
class="email-input"

Try
<%= form_for #user, :html => { :id => 'phx-signup-form' } do |f| %>
and
<%= f.text_field :email, :class => 'email-input' %>
You can read more references on http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html

A good place to start is the Rails API documentation.
form_for(#user, html: { id: 'phx-signup-form' })
should put you on the right path.

Your form automatically takes id as new_user if your form is running as new form and takes id as edit_user if your form is running as edit form. So you use id="new_user" or id="edit_user" instead of id="phx-signup-form" in your css depending on condition.
For your form and <%= f.text_field :email, "", :class => 'email-input' %> for emai field. Just try it. It may solve your problem.

Related

Rails - Show updated fields when editing

I am rolling my own authentication, and the issue I am running into is the edit form for my users. I here is the form...
#app/views/users/edit.html.erb
<h1>Editing User</h1>
<%= render 'form' %>
<%= link_to 'Show', #user %> |
<%= link_to 'Back', root_path %>
#app/views/users/_form.html.erb
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="field">
<%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel']) %><br/>
</div>
<div class="actions"><%= f.submit "Sign Up" %></div>
<% end %>
The issue I am having is that when I display the edit form the :user_type field is not correct. I want this field to reflect what is currently saved for the current user, instead I just get the drop down with the first option in the list displayed.
You want to either use:
<%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel'], #user.user_type) %>
or:
<%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel']), :selected => #user.user_type %>
options_for_select has second parameter to preselect it...
options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel'], #user.user_type)

How to add custom validation error message adjacent to input fields in Rails 3?

My Code in View Page(views/session/new.html.erb) :
< %= form_for(:session, :url => sessions_path) do |f| %>
<%= f.label :email, 'Username'%>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.submit 'Login'%>
<%= link_to 'Forgot Your Password?', new_password_reset_path >
< % end %>
I have written proper validation codes in my model file.
But I want that on Submit, the validation errors should pop up at the adjacent to the fields rather than at the top of the Page.
Thanks in Advance !
If you're inclined to use Rails' built-in server side validations, you can check for whether validation errors exist, then print them if they do:
<%= form_for(:session, :url => sessions_path) do |f| %>
<%= f.label :email, 'Username'%>
<%= f.text_field :email %>
<% if f.object.errors[:email] %>
<%= f.object.errors[:email].join(", ") %>
<% end %>
<%= f.label :password %>
<%= f.password_field :password %>
<% if f.object.errors[:password] %>
<%= f.object.errors[:password].join(", ") %>
<% end %>
<%= f.submit 'Login'%>
<%= link_to 'Forgot Your Password?', new_password_reset_path >
<% end %>
Note a few things:
First, because you're passing a symbol, rather than an object in your form, you'll need to access the object's attributes from within the form_for block by invoking f.object.
Second, because some fields may yield more than one validation error, you'll want to print them out in sequence. Using Ruby's join method can help you concatenate these in a coherent manner.
UPDATE:
If you want the errors to render directly beside the corresponding text field, you'll need to apply some CSS. Clearly, it's recommended that a stylesheet be used to for this, but in the interest of simplicity, here's of an example of how this might be rendered using inline styling:
<%= f.text_field :email, :address, :style => "display: inline-block; float: left;" %>
<% if f.object.errors[:email] %>
<span style="display: inline-block; float: left;">
<%= f.object.errors[:email].join(", ") %>
</span>
<% end %>
<div style="clear: left;"></div>
I'd go with something like this:
<%= form_for(:session, :url => sessions_path) do |f| %>
<%= f.label :email, 'Username' %>
<%= f.text_field :email %>
<%= f.object.errors[:email].to_sentence if f.object.errors[:email] %>
<% end %>
I reckon you'll figure out the rest.
You can access the values of error messages of different model attributes with errors hash like errors[:some_attribute_name]. As long as your custom validators store error message in the right model attribute, you can probably do something like this.
< %= form_for(:session, :url => sessions_path) do |f| %>
<%= f.label :email, 'Username'%>
<%= f.text_field :email %>
<%= f.object.errors[:email] %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.object.errors[:password] %>
<%= f.submit 'Login'%>
<%= link_to 'Forgot Your Password?', new_password_reset_path >
< % end %>
I'll let you worry about how the error is displayed beside the form and stuff. :)

rails form_for nested data doesnt show up

New to rails and getting confused on how to handle this. I am using rails 4. I am very stuck here and will try to talk through this problem.
I have a listings page which I am trying to add tags too. In my view (listings/new.html.erb) I have the following:
<h1> POST A NEW LISTING </h>
<% if current_user.nil? %>
<h2>You must be logged in to view this page </h2>
<% else %>
<%= form_for [#user, #listing] do |f| %>
<%= f.label :title, 'Title' %> <br />
<%= f.text_field :title %>
<%= f.label :general_info, 'General Information' %> <br />
<%= f.text_area :general_info %>
<%= f.label :included, 'Included' %> <br />
<%= f.text_field :included %>
<%= f.label :length, 'Length' %> <br />
<%= f.text_field :length %>
<%= f.label :price, 'Price' %> <br />
<%= f.text_field :price %>
<% fields_for #tagging do |u| %>
<%= u.label :tag, 'Tag' %> <br />
<%= u.text_field :tag %>
<% end %>
<%= f.submit "submit" %>
<% end %>
<% end %>
The form works correctly for putting in the listing, but the tagging form options do not even appear to allow for content.
my listings_conroller #new looks like this:
def new
if (!current_user.nil?)
#user = User.find(current_user.id)
#listing = #user.listings.build
#tagging = #listing.taggings.build
end
end
I want to be able to create a new listing with this form that also populates the database for the tags and I am very unsure on how to do this. I hope this is enough information, but if needed I have all the code here: https://bitbucket.org/r-s/ath/src . Very stuck on this any help would be appreciated.
Change it to:
<%= f.fields_for #tagging do |u| %>
Note the =.
You forgot to use builder:
<%= f.fields_for #tagging do |u| %>

AutoPopulate Form Values on Edit With Rails

I have a form that I copied over from new.html.erb and put it in edit.html.erb. I essentially want the same form, but if there are values already in the database for the form fields I want them to be pulled into the form for editing. I currently have something like this:
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
.
.
.
.
<div class="actions"><%= f.submit %></div>
<% end %>
How can I pull from the the database to fill in the fields with appropriate values?
This will happen automatically if you set #user in the 'edit' action of your controller - something like
def edit
#user = User.find(params[:id])
end
you might also think about including the form as a partial rather than repeating it to DRY up your code - this link gives the general idea

How do you call a from partial from different model in a layout/header

In Rails how do I call a form from another model in any given layout? I have a login form I want to put in the header of every page. I created a partial with the following in it:
<% form_for(#user_session) do |f| %>
<p>
<%= f.label :username %><br />
<%= f.text_field :username, :class=>'' %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<%= f.check_box :remember_me %><%= f.label :remember_me %><br />
<br />
<p>
<%= f.submit 'Login' %>
</p>
<% end %>
then tried calling that partial in my header and that doesn't seem to work.
<%= render :partial => 'user_sessions/login' %>
I get a "Called id for nil" error
<%= render :partial=> 'user_sessions/login' :layout => false%>
Use this
I think you save your partial with '_login.rhtml' like ?
Why not just <%= render #user_session %> ?
Is the #user_session variable set up within all of your controller actions?
Got the answer from jmesserer over at railsforum, just needed to change:
<% form_for(#user_session) do |f| %>
to
<% form_for UserSession.new do |f| %>

Resources