Get ID of parent object in ActiveAdmin / Formtastic - ruby-on-rails

I have a resource Photos, which belongs to Adverts.
In ActiveAdmin, users should be able to upload Photos directly from the Advert's edit page (obviously only once the advert has been created).
The form is generated as follows:
form do |f|
[... the usual forms ...]
f.inputs "Photos" do
f.has_many :photos, :title => "Photo" do |p|
p.input :advert, :as => :hidden, :value => Advert.find(params[:id])
p.input :title
p.input :image
end
end
end
I would like the line
p.input :advert, :as => :hidden, :value => Advert.find(params[:id])
to produce a hidden field with the ID of the Advert the user is editing, however this just produces an empty field. I've tried a number of other options as well, but can't seem to figure it out.
Any hints?

You can use advert variable, which holds Advert object with id from params.
p.input :advert, :as => :hidden, :value => advert.id
by the way, your code is invalid. You get whole Advert object, not just id. Valid code:
p.input :advert, :as => :hidden, :value => Advert.find(params[:id]).id
this should works, too

Related

Formtastic - Why does not work

I have tried to use this on my test
<%= f.input :user, :label => 'Usuario: ' , :as => :select , :collection => #users , :include_blank => true %>
And didnt work....
But when a switch to:
<%= f.input :user_id, :label => 'Usuario: ' , :as => :select, :collection => Hash[#users.map{|b| [b.nickname,b.id]}] , :include_blank => true%>
It does work? Does anyone knows why?
I also put it all together on the code:
<%= f.input :user, :label => 'Usuario: ' , :as => :select , :collection => #users , :include_blank => true %>
<%= f.input :user_id, :label => 'Usuario: ' , :as => :select, :collection => Hash[#users.map{|b| [b.nickname,b.id]}] , :include_blank => true%>
But the first one didnt work (i did just to see if was somente wrong in the #user variable)..
Is something related to the model? Does anyone knows why?
From formtastic documentation: Many inputs provide a collection of options to choose from (like :select, :radio, :check_boxes, :boolean). In many cases, Formtastic can find choices through the model associations, but if you want to use your own set of choices, the :collection option is what you want. You can pass in an Array of objects, an array of Strings, a Hash…
You can check the documentation
also collection is expect to receive a hash or array , but when you pass #user you pass an instance variable of your model and that will not work.

ActiveAdmin has_many relationship does not save new records?

I have the relation ContentRelease has_many pages. The following activeadmin registered model lets me edit properties of a page inside the content_release, but not create new pages for the content release. There is no error, I get a success message, but a new page is not saved. What am I doing wrong?
ActiveAdmin.register ContentRelease do
form do |f|
f.actions
f.inputs "Details" do
f.input :active_at
end
f.inputs "Pages", :class => 'pages-list' do
f.has_many :pages do |page|
page.inputs "Associated Look" do
page.input :title
page.input :content_release, :as => :select, :collection => ContentRelease.list
page.input :look, :as => :select, :collection => Look.current
page.input :share_url
end
end
end
f.inputs "Looks", :class => 'looks-list' do
f.has_many :looks do |look|
look.inputs do
look.input :title
end
end
end
f.actions
end

Rails form - How to create a nested has many form?

Currently I have a has_one relationship betweeen Users and photos.
User model:
has_one :photo
accepts_nested_attributes_for :photo
Photo model:
belongs_to :user
Paperclip.options[:command_path] = "/usr/local/bin"
has_attached_file :image,
:path => ':rails_root/public/images/ads/:id/:basename.:extension',
:url => "images/ads/:id/:basename.:extension"
The nested form:
<%= f.simple_fields_for :photo_attributes, :html => { :multipart => true } do |d| %>
<%= d.input :billed_navn %>
<%= d.label :image, :label => 'Upload logo', :required => false %>
<%= d.file_field :image, :label => 'Image', :class => 'imagec', :required => 'false', :style => 'margin-bottom:2px;float:left;width:250px;' %>
<input type="button" value="Clear" id="clear" style="width:70px;float:left;margin-right:2px;">
<%= d.input :image_url, :label => 'Billed URL', :input_html => { :class => 'imagec'}, :required => false %>
<%= f.label :image, :label => 'Billed preview', :required => false %><div id="preview"></div>
<% end %>
This setup works as it should, I can upload 1 photo.
I users to be able to upload multiple photos at once.
Therefor I have changed the assocition in useres model to:
User model:
has_many :photos
accepts_nested_attributes_for :photos
But I how should the nested form then be? If it should be possible to upload mulitple images at once?
The accepts_nested_attributes_for thing only allows mass assignment to add multiple photos at once. (Beware of mass assignment security vulnerabilities! strong_parameters gem recommended). This means that the update action accepts multiple photos.
It will only add it if they are sent, which happens if there are fields in the form that a user fills out. This is mainly determined by the edit view.
Because you don't know how many photos a user will want to add, the best way to do this is to use javascript to dynamically add an extra set of fields for a photo when requested by the user. This can be a link, which when clicked, appends the fields to the form. This way the user can submit as many photos at once as they want.
You will also want to have some validation so that if a set of empty fields (for a photo) are submitted, it doesn't add a non-photo photo.
If you don't want to use javascript, the best you can do is to just assume the user will upload at most say 3 at a time, and include 3 sets of photo fields. Again, being careful to deal with empty fields appropriately.
Example:
<% (1..5).each do |I| %>
<%= fields_for "user[photo_attributes][]", nil, :index => I do |form| %>
<%= form.input :billed_navn %>
...
<% end %>
<% end %>

Adding a custom input field in formtastic?

I can't figure out, or find any solutions to a very simple question:
"How can I define my own input field in formtastic?"
This is what I got:
<%= semantic_form_for #someFantasticVariable, :url => "/someFantasticUrl.html" do |f|%>
<%= f.inputs do %>
<%= f.input :something_else_id, :required => true , :as => :select, :collection => SomethingElse.find(:all), :label =>"The something else"%>
<%= f.input :fantastic_max_cost, :label => "Budget (max cost)"%>
<%end%>
<%= f.buttons do%>
<%= f.commit_button :button_html => { :class => "primary", :disable_with => 'Processing...', :id => "commitButton"}%>
<%end%>
<%end%>
Now..
I want to have a very simple thing. I want to ad a field that is not part of the model. I want to have a date field that I can use to calculate some stuff in my controller. So I want to do this:
<%= f.inputs do %>
<%= f.input :something_else_id, :required => true , :as => :select, :collection => SomethingElse.find(:all), :label =>"The something else"%>
<%= f.input :fantastic_max_cost, :label => "Budget (max cost)"%>
<%= f.input :start_date, :as => :date , :label => "Start date"%>
<%end%>
But apparetly I'm not allowed, and I can't find any way to do this through my thrusted googling. Any help / ideas?
If you have some attribute that is not part of your model, then a getter and a setter should exist on the model:
def start_date
end
def start_date=(arg)
end
Then you can calculate your staff on a controller or whatever you want:
...
puts params[:somefantasticvariable][:start_date]
...
But this is a quick formtastic hack, you should find some better way, like non-formtastic input with some css etc.
Ruby provides a database-less construct called an attr_accessor. It is the equivalent of writing setter and getter methods. Formtastic will see this attribute similar to a database-backed attribute.
In your #someFantasticVariable model:
attr_accessor :start_date
If using attr_accessible in your #someFantasticVariable model, be sure to add the start_date variable there too:
attr_accessible :start_date
Because the attribute is type-less, Formtastic cannot derive the HTML input field to use. You will need to manually set the input type using :as. For your example:
<%= f.input :start_date, :as => :date_select %>
Cite:
http://apidock.com/ruby/Module/attr_accessor
https://github.com/justinfrench/formtastic#the-available-inputs

Rails/Formtastic Newbie: Setting display field for referenced "select" box

New formtastic user here.
I have a relationship user has_many clients
In formtastic, if I do something such as
f.input :employer
it returns a select box of all employer object references. I'd like to display (last name, first name) instead. I'm sure this is very simple, but I can't figure out exactly how to do it.. Any help appreciated.
These didnt work for me, however, this did:
<%= f.input :user, :label => "Author", :label_method => :username %>
Also a little cleaner ^^
Or, you can set the display method once and for all on the model itself:
(In employer.rb)
def to_label
email
end
Try
f.input :employers, :as => :select, :collection => Employer.find(:all, :order => "last_name ASC")
or
f.input :employers, :as => :select, :collection => Employer.collect {|e| [e.last_name, e.id] }

Resources