This is a rather small thing, but rather annoying.
When I submit a blank "create account" form (bringing up form validation errors & styles) and then navigate to another page and then back to the create account page the form validation errors are still there for a half second before the page refreshes.
Is there any way to clear these validations when the page is left so that they don't reappear for a half second when the page is navigated back to?
View Code:
<section class="form form_create_account">
<h2 class="form_header">Create Account</h2>
<%= form_for(#user, url: create_account_path) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<% if #user.errors[:first_name].any? %>
<p class="error_message"><%= #user.errors[:first_name].first %></p>
<% end %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<% if #user.errors[:last_name].any? %>
<p class="error_message"><%= #user.errors[:last_name].first %></p>
<% end %>
<%= f.label :email %>
<%= f.text_field :email %>
<% if #user.errors[:email].any? %>
<p class="error_message"><%= #user.errors[:email].first %></p>
<% end %>
<%= f.label :password %>
<%= f.password_field :password %>
<% if #user.errors[:password].any? %>
<p class="error_message"><%= #user.errors[:password].first %></p>
<% end %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<% if #user.errors[:password_confirmation].any? %>
<p class="error_message"><%= #user.errors[:password_confirmation].first %></p>
<% end %>
<%= f.submit "Create Account" %>
<% end %>
</section>
Sometimes rails do it automatically, but sometimes we need to do. When working with flash give special attention on render & redirect.
When you render, you want to set flash.now, instead of the normal flash.
(Ex: flash.now[:error] = "There was a problem" with render, not flash[:error] = "There was a problem".)
If you don't, then the flash message may not show up on the page that's rendered, and it will show up on the next page that's visited.
This answer is for not showing flash[:errors]. You can refer this with hiding flash as well.
Hope this will help
Related
I have set everything up for active storage and when I add a trip with an image, the app redirects to that trips show page and I can see the image. When I lave and go back to that show page I get the error
`Can't resolve image into URL: undefined method `persisted?' for nil:NilClass`
Here is the show page for trip
<h1> Location: <%= #trip.location %> </h1>
<h2> Date Visited: <%= #trip.date %> </h2>
<%= image_tag #trip.cover_photo, :style => "max-height:300px"%>
<h2> More Photos </h2>
<%= link_to "Add a photo", new_photo_path(#trip) %> <br>
<%= link_to "Public Profile", trips_path(#user) %> <br>
Here is the new trip form
<h1>Add a new trip </h1>
<%= form_for #trip do |f| %>
<%= f.label :location %>
<%= f.text_field :location %> <br>
<%= f.label :date %>
<%= f.text_field :date %> <br>
<%= f.label :cover_photo %>
<%= f.file_field :cover_photo %> <br>
<%= f.hidden_field :user_id, :value => session[:user_id] %>
<%= f.submit "Submit" %>
<% end %>
I'm not able to see the error since the photo uploads and displays correctly at first.
Had the same issue, except that I wasn't able to display the images even once. In your case, seems that the first time you are watching a temporal image, which is deleted after you live that view.
I realize that I was forgetting to add the attachment to permitted parameters in my controller, so the image wasn't saving. Your params should look like this:
def trip_params
params.require(:trip).permit(:location, :date, :cover_photo, :user_id)
end
I have started the "Get Started Guide" from the ruby on rails website. Everything works fine, but when I change the order of showing all comments and than display the comments-form in the other way round, than the form_forfunction adds a empty comments model to #post.comments and so, I display one empty comment in the loop.
Here is the view:
<h1><%= #post.name %></h1>
<p><%= #post.text %></p>
<h2>Add comment</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %><br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<h2>Comments</h2>
<%= render #post.comments %>
The loop display two comments. One, that exists in the db and one, that has just empty attributes. If I delete the form, than all is shown up correct.
You can select only your persisted comments:
<%= render #post.comments.select(&:persisted?) %>
When you do post.comments.build against some post, it will be added to the post.comments collection and will be displayed along with other comments.
You can always use persisted to check if the object is present in database meaning id is assigned to it.
#post.comments.select(&:persisted?)
Note: .present? check donot work here so you have to use .persisted.?
present check only assosiated to the parent.
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| %>
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.
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