semantic_form_for without resource with multiple input not working - activeadmin

i got some problem with a resourceless form inside activeadmin page:
ActiveAdmin.register_page 'TestDashboard' do
menu :label => 'TestDashboard'
content title: 'TestDashboard' do
columns do
column do
div do
semantic_form_for 'dashboard', :url => '#' do |f|
f.inputs :name => 'Configure', :class => 'inputs' do
f.input :target
f.input :name
end
end
end
end
end
end # content
end
only the last field (name) is shown. The resulting html is:
<form novalidate="novalidate" method="post" class="formtastic dashboard" action="#" accept-charset="UTF-8">
<div style="margin:0;padding:0;display:inline"><input type="hidden" value="✓" name="utf8">
<input type="hidden" value="3424234blabla" name="authenticity_token">
</div>
<fieldset class="inputs">
<legend>
<span>Configure</span>
</legend>
<ol>
<li id="dashboard_name_input" class="string input required stringish">
<label for="dashboard_name" class="label">Name<abbr title="required">*</abbr></label>
<input type="text" name="dashboard[name]" id="dashboard_name">
</li>
</ol>
</fieldset>
</form>
Thank you in advance.

You should put this in a partial:
In views/admin/testdashboard/_configure_form.html.erb
<%= semantic_form_for 'dashboard', :url => '#' do |f| %>
<%= f.inputs :name => 'Configure', :class => 'inputs' do %>
<%= f.input :target %>
<%= f.input :name %>
<% end %>
<% end %>
In your TestDashboard.rb
ActiveAdmin.register_page 'TestDashboard' do
menu :label => 'TestDashboard'
content title: 'TestDashboard' do
columns do
column do
render "configure_form"
end
end
end # content
end
And it should work nicely then :)

I have the same problem, and i use active_admin_form_for instead semantic_form_for
Example:
ActiveAdmin.register_page 'TestDashboard' do
menu :label => 'TestDashboard'
content title: 'TestDashboard' do
columns do
column do
div do
active_admin_form_for'dashboard', :url => '#' do |f|
f.inputs :name => 'Configure', :class => 'inputs' do
f.input :target
f.input :name
end
end
end
end
end
end # content
end

Related

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

change password bcrypt with hash and salt on ruby on rails 4.2

I'm trying to change the user password using the gem bcrypt and the hash-salt method.
Here's my code where i include my attempt to change password, but it gives me an error of a missing template.
User Controller
def create
#user = User.new(user_params)
end
def change_password
#user = User.find(params[:id])
if #user.password_hash == BCrypt::Engine.hash_secret(params[:current_password], #user.password_salt)
#user.password = params[:password]
#user.save
redirect_to "/users/#{#user.id}"
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
User Model
before_save :encrypt_password
def self.authenticate(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
return user
else
return nil
end
end
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
also, the Routes
patch 'users/:id/change_password' => 'users#change_password'
resources :users
and last but not less important, the form.
<%= form_for(#user, :url => "change_password") do |f| %>
<%= hidden_field(:user, :email, :value => #user.email) %>
<div class="form-group">
<div class="form-group col-md-4"><%= f.label :contraseña_actual %></div>
<div class="form-group col-md-8"><%= f.password_field(:current_password, :class => "form-control") %></div>
</div>
<div class="form-group col-md-4"><%= f.label :nueva_contraseña %></div>
<div class="form-group col-md-8"><%= f.password_field(:password, :class => "form-control") %></div>
<div class="form-group">
<div class="form-group col-md-4"><%= f.label :confirmar_contraseña %></div>
<div class="form-group col-md-8"><%= f.password_field(:password_confirmation, :class => "form-control") %></div>
</div>
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn btn-default">Cambiar Contraseña</button>
</div>
<% end %>

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.

Nested attributes of validation form field value cleared

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%>

Resources