Nested attributes of validation form field value cleared - attributes

I am having form of user.Where user table has username and email address and another table of user profile which has user_id,location etc..
i am using the form below
I have done the vaildation for user email_Addreess uniqueness.if validation happens then i render to the same page.
My problem was if validation happen and render to same page the values in the fields of city,locations value field gets cleared.but values remains in other fields.Pls re me soon
<%= form_for #user_usr, :url => { :action => "provider_submit" } do |f| %>
<%=params.inspect%>
<% #user_usr.build_user_profile_pfl%>
<% if #user_usr.errors.any? %>
<div id="error_explanation">
<ul>
<% #user_usr.errors.each_with_index do |msg, i| %>
<!--for valdiation -->
<li><%= msg[1] %></li>
<% end %>
</ul>
</div>
<% end %>
username<%= f.text_field :username_usr,:id=>"p_name",:name=>"p_name", :class=>"lt textbox_provider",:maxlength => 50,:style=>"outline: none;",:onclick =>"focusChangeBorderColor('p_name');",:onblur=>"blurChangeBorderColor('p_name')",:onchange=>"removeunwantedSpace(this.value)", :tabindex => '1'%>
email<%= f.text_field :email_usr,:id=>"p_email",:name=>"p_email", :class=>"lt textbox_provider",:maxlength => 50,:style=>"outline: none;",:onclick =>"focusChangeBorderColor('p_email');",:onblur=>"blurChangeBorderColor('p_email')",:onchange=>"removeunwantedSpace(this.value)", :tabindex => '2'%>
<%= f.fields_for :user_profile_pfl do |builder| %>
City <%= builder.text_field :city,:id=>"b_name",:name=>"b_name", :class=>"lt textbox_provider",:maxlength => 50,:style=>"outline: none;",:onclick =>"focusChangeBorderColor('b_name');",:onblur=>"blurChangeBorderColor('b_name')",:onchange=>"removeunwantedSpace(this.value)", :tabindex => '2'%>
<%end%>
<p><%= f.submit "Submit" %></p>
<%end%>

Related

File type:ejs.I have a problem with syntacxis

<ul>
<% if ('contacts'.length) { %> <% 'contacts'.forEach(({ link, name }) => { -%>
<li>
<%= name %>
</li>
<% }) %> <% } %>
</ul>
I have a syntacxis problem with this part of ejs file.What would be the correct syntax?
I try ejslint,but he doesnt support me

I want to redirect to another page when user click image

i created a gallery and i want when user click a image then it will redirect to another page as well as new page display full details of that particular image ! how to do it in node js and mongoDB.
This is my index.ejs page where i displayed my images.
<% if(files) { %>
<% files.forEach(function(file) { %>
<div class="card card-body mb-3">
<% if(file.isImage){ %>
<img src="image/<%=file.filename %>" alt="" >
<span><%= file.metadata.username %></span><br>
<span><%= file.metadata.age %></span>
<% } else {%>
<p>Error</p>
<% } %>
</div>
<% }) %>
<% } else { %>
<p>No files to show </p>
<% } %>
route file
app.get('/checkout/:filename', (req,res) => {
res.redirect('/solo', { pic: req.params.filename });
});

Rails 5 error message: Child-Model Parent-Model must exist

I have two models, Parent is Property, child is Phone. When attempting to create a new Property record with nested Phone data, I receive an error message: Phones property must exist.
I've studied the Rails Guide and a number of other documents without determining the cause. Here is a public github link if you want to see all the code: https://github.com/allenroulston/testnest.git
class Property < ApplicationRecord
has_many :phones
accepts_nested_attributes_for :phones
end
class Phone < ApplicationRecord
belongs_to :property
end
# the form accepting the data
<%= form_for(property) do |f| %>
<% if property.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(property.errors.count, "error") %> prohibited this property from being saved:</h2>
<ul>
<% property.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :address %>
<%= f.text_field :address %>
</div>
<div class="field">
<%= f.label :city %>
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label "Telephone (example: 613 555 1234 )" %>
<%= f.fields_for :phones do |p| %>
Area Code <%= p.text_field :area %>
Exchange <%= p.text_field :exchange %>
Number <%= p.text_field :number %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
# relevant controller methods ##################
# GET /properties/new
def new
#property = Property.new
#property.phones.build
end
# POST /properties
# POST /properties.json
def create
#property = Property.new(property_params)
respond_to do |format|
if #property.save
format.html { redirect_to #property, notice: 'Property was successfully created.' }
format.json { render :show, status: :created, location: #property }
else
format.html { render :new }
format.json { render json: #property.errors, status: :unprocessable_entity }
end
end
end
As far as I know, this is because when you use nested_attributes_for when creating a new object, the parent object is not yet created, so when attempting to create a reference to the parent object, the validation fails. To fix this you should change to: has_many :phones, inverse_of: :property.
You need to add in the Property model as :-
accepts_nested_attributes_for :phones, reject_if: :all_blank, allow_destroy: true

Having trouble correctly saving a nested form that has file_field

Using paperclip and ruby-mp3info to upload and read file info, I have a form that creates a playlist and its children, tracks:
<%= form_for(#playlist) do |f| %>
<% if #playlist.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#playlist.errors.count, "error") %> prohibited this playlist from being saved:</h2>
<ul>
<% #playlist.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Name: " %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label " Image: " %>
<%= f.file_field :photo %>
</div>
<br>
<div class="field">
<br>
<%= f.text_area :description, :size => "80x3"%>
</div>
<br>
<div class="field">
<%= f.fields_for :tracks, Track.new do |ff| %>
<%= ff.file_field :audio %>
<% end %>
</div>
<% if !#playlist.tracks.blank? %>
<table id="tracks" class="table">
<thead>
<tr>
<th>Track</th>
<th>Album</th>
<th>Artist</th>
<th>Label</th>
</tr>
</thead>
<tbody>
<%= f.fields_for :tracks do |ff| %>
<%= render "track_fields", :f => ff %>
<% end %>
</tbody>
</table>
<% end %>
<div class="actions">
<%= f.submit "Save" %>
</div>
<% end %>
The problem with uploading a file is that I won't have it until I click on the Save button. So I read the info from within my controller. Within the update method of my Playlist controller, I try to create a new track by the following code:
def update
track = #playlist.tracks.new()
Mp3Info.open(playlist_params["tracks_attributes"]["0"]["audio"].path.to_s) do |info|
track.audio_file_name = info.tag.title
track.artist = info.tag.artist
track.album = info.tag.album
# tracknum = info.tag.tracknum.to_s
end
respond_to do |format|
if #playlist.update(playlist_params)
format.html { redirect_to #playlist, notice: 'Playlist was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #playlist.errors, status: :unprocessable_entity }
end
end
end
The problem is that the code in my update method will create TWO instances of tracks when I only want one. How do I create only the instance that I am creating from within my controller and ignore the new track that is being created from within the form?
Are there alternative ways to do this?
You are getting two records in tracks table because one you are creating from form (with track_attributes) and other you are creating in update action.
If you just need to update few properties of the track to be saved then do it on the track coming in params[:playlist][:tracks_attributes].
Change the update action as below:
def update
Mp3Info.open(playlist_params["tracks_attributes"]["0"]["audio"].path.to_s) do |info|
params[:playlist][:tracks_attributes]["0"][:audio_file_name] = info.tag.title
params[:playlist][:tracks_attributes]["0"][:artist] = info.tag.artist
params[:playlist][:tracks_attributes]["0"][:album] = info.tag.album
# tracknum = info.tag.tracknum.to_s
end
respond_to do |format|
if #playlist.update(playlist_params)
format.html { redirect_to #playlist, notice: 'Playlist was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #playlist.errors, status: :unprocessable_entity }
end
end
end
This will create only one associated Track record for a Playlist in tracks table.

Search result displays on wrong page

I am having a bit off difficulty implementing the ransack gem I have a pages controller with an index action and post controller also with an index action. However when I perform a search in the index action of the pages controller (pages#index) the results are rendered in the index action of the post controller(posts#index). Does this mean i can only search from within the views of a particular resource or am I making a mistake ?
pages#index
def index
#q = Post.search(params[:q])
#posts = #q.result(distinct: true)
end
pages#index
<%= search_form_for #q do |f| %>
<div class="field">
<%= f.label :title_cont %><br>
<%= f.text_field :title_cont, :class => "input text" %>
</div>
<div class="actions">
<%= f.submit "Search"%>
</div>
<% end %>
<%= search_form_for #q,:url=>pages_path do |f| %>
<div class="field">
<%= f.label :title_cont %><br>
<%= f.text_field :title_cont, :class => "input text" %>
</div>
<div class="actions">
<%= f.submit "Search"%>
</div>
<% end %>
Modify the code in the search form like this. It will work now.

Resources