I have a rails 3 application which uses the ActiveAdmin gem.
Is there a way to make a form in the dashboard page. A form that is not related to any models ? In my case, a form to select time period to display some stats ?
I have tried :
form do |f|
f.inputs "test" do
f.input :time, label: "Duration", as: :select, collection: [['24h', 24], ['1 week', '1w'], ['1 month', '1m']]
end
f.actions
end
But i'm getting an "undefined methods errors" for f.inputs. Does someone have an idea ?
You probably need to create a form partial like this:
-# admin/whatever/_form.html.haml
= semantic_form_for 'whatever', :url => admin_whatevers_path do |f|
= f.inputs :name => 'tests' do %>
= f.input :time, collection: [['24h', 24], ['1 week', '1w'], ['1 month', '1m']]
and tell activeadmin to use the partial like this:
#admin/whatever.rb
form :partial => 'form'
Related
i got the following issue:
I want to add a form on a custom page (no model) to design an email for mailjet. I already got a custom page and a form with an CKEditor input and an input for the subject. But the subject field isn't shown on the page.
This is my code:
ActiveAdmin.register_page "Mail", namespace: :lku do
def send_mail
end
content do
panel 'Write mail' do
semantic_form_for :mail, :url => "lku/send_mail", method: :post do |f|
f.inputs do
f.input :subject, :input_html => { :name => 'subject' }
end
f.inputs do
f.input :text, as: :ckeditor, :input_html => { :name => 'text' }
end
end
end
end
end
And this is the result:
When I add
f.actions
it looks like this
can someone help me please?
I was able to reproduce the problem without having ckeditor. Only the last element of form would display.
I don't know exactly what's going on, but it has something to do with how Arbre renders the content you generate inside the content block.
My solution is this
Transform the form content into .erb and move the form under views/lku/mail/_mailform.html.erb
<%= semantic_form_for :mail do |f| %>
<%= f.inputs do %>
<%= f.input :subject %>
<%= f.input :text %>
<% end %>
<%= f.actions %>
<% end %>
Include the form in the page
content do
panel 'Write mail' do
render partial: 'mailform'
end
end
See if you can still mount the editor by using regular Rails form helpers - https://github.com/galetahub/ckeditor#form-helpers
How can I add two label elements when choosing association in Simple Form on Ruby on Rails?
Sample: #user.name = "Barack" and
#user.last_name = "Obama"
Here is my code:
<%= f.association :persona, :collection => Persona.order(:name),
:prompt => 'Choose a person' %>
It displays only Barack but I need it to display not only name but also last_name when choosing from list.
<%= f.association :persona, :collection => Persona.order(:name), :label_method => lambda { |persona| "#{persona.name} #{persona.last_name}" }, :prompt => 'Choose a person'%>
Here is the answer - you need a complex label_method.
Im trying to design a shopping cart. i.e a customer shopping online adds a product to their trolley.
I want to go straight to create action from my new action without going to new.html.erb with pre-set values in my params
Here is what I have so far:
#trolley_id += 1
redirect_to :controller => 'trolleys', :action => 'create', :id => #trolley_id, :something => 'else', method: :post
This redirects me to my index action
To do this with javascript templates, it would look like this:
view
= form_form Trolley.new, remote: true do
-# form goes here
The remote true will submit it as javascript, which will try to render a javascript template.
Your create action can either render :create or let Rails render your template automatically. Since it came in as a javascript request, Rails will render the template with format js.
trolleys/create.js.erb
var html = "<%= j render 'trolley_row', trolley: #trolley %>
$('table.trolleys body').append(html);
I managed to resolve my problem. I created a form in my Product_controller#show that will go straight to my Trolley_controller#create and create an entry in my Trolleys table
<%= simple_form_for [#product, #trolley] do |f| %>
<%= f.input :quantity, collection: 1..12, prompt: "select quantity" %>
<%= f.input :product_id, :as => :hidden %>
<%= f.input :user_id, :as => :hidden %>
<%= f.button :submit, "Add to Basket" %>
<% end %>
I am editing a Rails 2 application. In it, a user submits a form that includes a dropdown menu, and that information creates a record in my database. When the user goes to edit the record, I want to show the saved "selected" option in the dropdown menu. I keep getting errors, however. Here is my set-up:
View Select Field
<% form_for #newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
<%= f.select :start, options_for_select(#itinerary.locations), {:include_blank => true}, {:id=>"startdrop" } %>
Form Helper
def options_for_select(locations)
locations.map do |l|
content_tag "option", l.masterlocation.name, location_option_attributes(l)
end.join("\n")
end
private
def location_option_attributes(location)
{
:value => "#{location.masterlocation.street_address}, #{location.masterlocation.city}, #{location.masterlocation.state}, #{location.masterlocation.zip}",
:id => location.masterlocation.id,
:"data-masterlocation-name" => location.masterlocation.name,
:"data-masterlocation-id" => location.masterlocation.id,
:"data-masterlocation-latitude" => location.masterlocation.latitude,
:"data-masterlocation-longitude" => location.masterlocation.longitude
}
end
I have tried making the view look like this:
<%= f.select :start, options_for_select(#itinerary.locations, #newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
But I get wrong number of arguments (2 for 1)) for that line. Also tried
<%= f.select :start, options_for_select(#itinerary.locations), {:selected => #newsavedmap.start, :include_blank => true}, {:id=>"startdrop" } %>
But nothing is preselected when I go to edit the saved map. I've tried following these links, and keep striking out. Appreciate any help you have to offer!
Rails select helper - Default selected value, how? , Rails form_for select tag with option selected , Rails select helper - Default selected value, how?
You could try something like this in your helper
def options_for_select(locations, selected=nil)
locations.map do |l|
tag_options = location_option_attributes(l)
if l == selected
tag_options[:selected] = "selected"
end
content_tag "option", l.masterlocation.name, tag_options
end.join("\n")
end
then call field like you were trying before.
<%= f.select :start, options_for_select(#itinerary.locations, #newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
You can pass one more parameters to select the value like this:
<%= f.select :start, options_for_select(#itinerary.locations), :selected => #newsavedmap.start, {:include_blank => true}, {:id=>"startdrop" } %>
I have a two select fields and their default options are blank so I would like to set all of them to 'Choose one'. I can do it for individual fields but I want to do it in the config somehow (avoiding the redundancy).
form do |f|
f.inputs "Item" do
f.input :field_1, :prompt => 'Choose one', :foo
f.input :field_2, :prompt => 'Choose one', :bar
end
end
How do I do this? :)
Edit: These prompts would be used on many forms. It really needs to be a config thing.
Try using a array:
fields = [:field_1, :field2]
form do |f|
f.inputs "Item" do
fields.each { |field| f.input field, :prompt => 'Choose one' }
end
end