password value does not appear inside textbox on edit - ruby-on-rails-3

I have a non-user model and it has attrs/db-columns, as "password" and there is one more "other password", for both when edited that object, their value do not appear inside textboxes
I did not find any suspicious code inside activeadmin-0.5.1 which would cause this.
Any hints?
Even if I use defaults or put the following, I get the same result
form do |f|
f.inputs "Details" do
f.input :user, :as => :select
f.input :type, :input_html => { :disabled => 'disabled' }
f.input :password
f.input :extra
f.input :other_password
end
f.actions
end
Seems like some filter on any field being edited having 'password' in its name ?

This is most likely due to formtastic's inferred field types, turning any field matching 'password' to be of type :password. Try setting those inputs using the :as => :string option:
form do |f|
f.inputs "Details" do
f.input :user, :as => :select
f.input :type, :input_html => { :disabled => 'disabled' }
f.input :password, :as => :string
f.input :extra
f.input :other_password, :as => :string
end
f.actions
end

Related

How to show two 'f.input' in one line in activeadmin form page

form do |f|
f.inputs :question do
f.input :id, :as => :hidden
f.input :questionaire_id, :as => :hidden
f.input :role, :as => :hidden
f.input :question_type
f.input :description
f.input :option
f.input :score
end
f.actions
end
In above form, how to show this two inputs
f.input :option
f.input :score
in one line? Any idea?
I think I got the answer
First we should check this Formtastic::Helpers::InputHelper
According to the helper:
input is used to render all content (labels, form widgets, error messages, hints, etc) for a single form input (or field), usually representing a single method or attribute on the form's object or model.
The content is wrapped in an li tag, so it's usually called inside an inputs block (which renders an ol inside a fieldset).
It's option :wrapper_html can be used to override or add to the HTML attributes to be passed down to the wrapping li tag
So in the form we should do this:
form do |f|
f.inputs :question do
f.input :id, :as => :hidden
f.input :questionaire_id, :as => :hidden
f.input :role, :as => :hidden
f.input :question_type
f.input :description
f.input :option, :wrapper_html => { :class => 'fl' }
f.input :score, :wrapper_html => { :class => 'fl' }, :label => false
end
f.actions
end
:label => false can disabled the label of form input.
And then specify the class 'fl' in the css file(I just add following css from Include two inputs in same LI element in Formtastic in active_admin.css.scss like #Andrey Deineko told):
form.formtastic fieldset ol li.fl {display:inline;}
Then we have two inputs on the same line~Hope this will be helpful~

Memory (hex) address being returned instead of value in Rails

I'm definitely a bit of a noob, so this might be something simple that I'm overlooking, however, the searches that I've done to try and find a solution have come up empty.
I've built a form using formtastic that has 5 input fields: two are text boxes and three are select lists.
<%= semantic_form_for #player do |f| %>
<%= f.inputs do %>
<%= f.input :firstname, :label => "First Name " %>
<%= f.input :lastname, :label => "Last Name " %>
<%= f.input :leagueid, :as => :select, :collection => League.all(:order => :leaguename), :label => "League " %>
<%= f.input :team_1, :as => :select, :collection => Team.all(:order => :name), :label => "Team 1 " %>
<%= f.input :team_2, :as => :select, :collection => Team.all(:order => :name), :label => "Team 1 " %>
<% end %>
<%= f.actions %>
<% end %>
What is happening is that the Teams lists work perfectly (the team names are displayed). However, the League list is a different story. All of the entries in the list look like this (with different a different code after 'League:'):
#<League:0x007fe29c406498>
If I use the form to create a Player, it works fine. The correct league ID goes into the database and everything. I just can't figure out why the names of the teams show, while whatever-that-is shows for the league.
Any and all help is appreciated.
When converting objects to String, Ruby will convert them to the memory address like you see unless you provide a to_s method for string conversions. I haven't used formtastic, but I believe adding a to_s method to your League class should cause it to display what you want.
Try adding
def to_s
#name # use whatever you want to be displayed.
end
to the League class.
You could try explicitly specifying the fields that should be used as the text and id within the select list. I believe it would look like.
<%= f.input :leagueid, :as => :select, :collection => Hash[League.all.map { |league| [league.leaguename, league.id] }]
The syntax is crazy. The call to map is returning an array of name/id pairs, like [ ['league1', 1], ['league2', 2] ]. Calling Hash on that converts it to a hash, like {'league1' => 1, 'league2' => 2}. Seems like the select list should use this hash to populate itself.
There's an example of this at http://rdoc.info/github/justinfrench/formtastic, under the Usage section.
:member_name is the solution I think.
<%= f.input :leagueid, :as => :select, :collection => League.all(:order => :leaguename), :label => "League " %>
Will probably work for you as
<%= f.input :leagueid, :as => :select, :member_name => :league, :collection => League.all(:order => :leaguename), :label => "League " %>
My problem was I have a model with a field the same name as the model and I think that confused Formtastic
Example scaffold:
rails g scaffold Countries code:string country:string
rails g scaffold Types title:string description:string
Model:
class Sign < ActiveRecord::Base
attr_accessible :title, :country_id, :type_id
belongs_to :country
belongs_to :type
Form View:
<%= f.input :type %>
<%= f.input :country, :member_label => :country %>
Having a form without the member_label leads to the object id displaying in the select box for countries although the ID is correctly saved. The type select worked perfectly without declaring member_label.
Note that I didn't need to specify :as => :select as formtastic can deduce this from the belongs_to relationship.
Try using :
<%= form.input :league, :member_label => :leaguename %>
This will override the naming convention of the Formtastic Column Select.
Have a look also at : Overriding the Column Name Convention wiki
Hope this helped.

How to display only the value in edit page in Active Admin

I have a edit form in Active Admin. I need some field as read only.
My current edit page is like
I need the page look like this
How can this be done. My code for the edit form page is like
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.input :current_address, :label => 'Current Address', :as => :string
end
end
Please help.
As Alex said, set to disabled. You could then use css to get the visual you wanted, if you can live with the semantics of that.
The syntax was slightly different for me to get this to work.
in your admin form:
f.input :finish_position, input_html: { disabled: true }
in your CSS active_admin.css
input[disabled="disabled"],
input[disabled] {
background-color: #F4F4F4;
border: 0px solid #F4F4F4 !important;
}
For a cleaner form definition within your ActiveAdmin.register{} block you may as well want to define a "readonly" input type to be used within active admin using formtastic:
Form block syntax is for activeadmin version 1.0.0.pre at 0becbef0918a.
# app/admin/inputs/readonly_input.rb
class ReadonlyInput < Formtastic::Inputs::StringInput
def to_html
input_wrapping do
label_html <<
template.content_tag('div', #object.send(method))
end
end
end
# app/admin/your_model.rb
ActiveAdmin.register YourModel do
# ...
form do |f|
# ...
input :current_address, as: :readonly
# ...
end
end
I was facing the same issue and tried using :disabled but it did not solve my problem as I wanted field value to be included in params object while sending it to the server. When you mark a form input as :input_html => {:disabled => true} , it does not include this field value in params.
So, instead I used :input_html => {:readonly => true} which solved both of my problems:
Does not allow user to edit
Includes the value in params
I hope this will help.
How about this?
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.li do
f.label :current_address
f.span f.object.current_address
end
end
end
Try to add , :disabled => true for address input field.
The trick is to use "object". Here is how you should code it:
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.label :current_address, f.object.current_address
end
end

Default value on select field in formtastic form with no model

I have a formtastic form to gather parameters for report generation. I know formtastic is designed to be used with models but I need to use it here as the form is in an activeadmin page.
It's all working well but I can't set a default value for the selects. I'm willing to implement a "despicable hack" to get this working. I'd prefer not to implement a fake model
just to set default values on a form.
The form code looks like this
<%= semantic_form_for "report", :url => report_admin_payments_path do |f| %>
<%= f.inputs do %>
<%= f.input :report_type, :as => :select, :collection => report_types, :input_html => {:style => "width:180px"} %>
<%= f.input :start_date, :as => :string, :input_html => {:class => 'datepicker', :style => "width:60px", :value => 1.month.ago.strftime("%Y-%m-%d")} %>
<%= f.input :end_date, :as => :string, :input_html => {:class => 'datepicker', :style => "width:60px", :value => Time.zone.now.strftime("%Y-%m-%d")} %>
<%= f.input :country, :as => :select, :collection => locales, :input_html => {:style => "width:180px"}%>
<% end %>
<%= f.actions :submit %>
<% end %>
Thanks in advance,
Matt
This or something similar should meet your needs.
class LightModel
# Subclass this for a model that looks like an ar model but has no persistence
# good for formtastic forms
include ActiveModel::Naming
include ActiveModel::Validations
def initialize(attributes = {})
#attributes = attributes
end
# read only atm
def method_missing(m, *args, &block)
#attributes[m]
end
end
Thanks,
Matt

undefined method `has_many' for Formtastic

I'm getting this error :
undefined method `has_many' for #<Formtastic::SemanticFormBuilder:0xb410d4c>
It work when I use it like this :
ActiveAdmin.register Ressource do
form do |f|
f.inputs do
f.input :offer_id, :as => :hidden
f.input :name
f.input :category, :include_blank => false, :collection => Category.order('place ASC').all, :member_label => :to_label
f.input :description, :input_html => {:class => 'editor'}
f.input :price
end
f.has_many :roles do |app_f|
app_f.inputs do
if not app_f.object.id.nil?
app_f.input :_destroy, :as => :boolean, :label => "Supprimer l'utilisateur du lot"
end
app_f.input :user, :member_label => :to_label, :label => 'Assigné le lot'
app_f.input :name, :include_blank => false
end
end
f.buttons
end
end
But it doesn't work in a partial, i need to call the has_many method by a different way or something else ?
ActiveAdmin extends formtastic with some useful helpers such as has_many (lib/active_admin/form_builder.rb in the activeadmin gem).
Unfortunately, these helpers are not available by default in your templates.
Here are two options:
If you don't need the extra has_many functionality (it looks like active_admin adds some javascript to make it easy to add a new record to the collection), then you can use stock formtastic. This example should work fine in the activeadmin file as well as in a partial:
ActiveAdmin.register Ressource do
form do |f|
# ...
f.inputs :for => :roles do |app_f|
# ...
app_f.input :name, :include_blank => false
end
f.buttons
end
end
Use the ActiveAdmin form builder explicitly:
<%= semantic_form_for [:admin, #resource], builder: ActiveAdmin::FormBuilder do |f| %>
<!-- ... -->
<%= f.has_many :teachers do |app_f| %>
<%= app_f.inputs do %>
<!-- ... -->
<% end %>
<% end %>
<%= f.buttons %>
<% end %>
I hope this helps.
There is a solution
form :html => {:multipart => true} do |f|
end
Or, if you want to use partial:
<%= active_admin_form_for [:admin, #resource] ,:html => {:multipart => true} do |f|%>
<% end %>