Rails 3: User Created custom forms?
I assume you have some kind of Form
model, and then some kind of Field
model, and a Form has_many :fields
. Correct?
Building the form is than quite straightforward: retrieve the form, iterate over all the fields, and depending on the type, render the correct code. If you use something like formtastic or simple_form the code is pretty straightforward.
But to make it work, inside your controller you will have to create a dummy object, that has a getter and setter for all fields. You could use a simple hash for this, or OpenStruct (better). While iterating over the fields set the hash with empty or default values.
I think you also want to save the results of a form? I think the easiest way is to use a model like this
t.form_id :integer
t.fields_data :text
And store the entered data in the text-field in e.g. json or something.
You could also do something like
class FormData
belongs_to :form
end
class FormDataField
belongs_to :form_data
belongs_to :form_field
end
while this is the cleanest (you can query on values of filled in fields for a certain form), it is maybe too much of an overhead.
Custom Form Builder in Rails 3
The Rails source you are referring to shows that text_field
has two parameters not three. See FormBuilder
source from github.
So update your form builder class as:
class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
def text_field(method, options = {})
super(method, options)
end
end
Then the usage of this, including the class
option you want to add:
<%= form_for :foo, builder: BootstrapFormBuilder do |f| %>
<%= f.text_field :foo_field, class: 'bootstrap_text_field' %>
<% end %>
Any options you pass in to the default FormBuilder
are also available to this child class, so use the class
option as you would for default form builder.
How to Generate Custom Forms with Builders in Rails
You can use dynamic form builder. Follow this github link. Hope it is useful.
https://github.com/railscasts/403-dynamic-forms
Rails 3 - Forms, Custom Submit Button based on Action?
Yes, this is handled in Rails with i18n by default. The translations are in ActionView, and look like this:
en:
helpers:
select:
# Default value for :prompt => true in FormOptionsHelper
prompt: "Please select"
# Default translation keys for submit FormHelper
submit:
create: 'Create %{model}'
update: 'Update %{model}'
submit: 'Save %{model}'
All you need to do is f.submit
(without passing "Create" or "Edit", etc) and the translations will do the rest. You can overwrite these by dropping the above yaml into your local locales.
If you need to set the class you can pass nil, e.g. f.submit nil, :class => 'whatev'
Custom Fields/Forms in Ruby on Rails
If using a relational database, here is one way to accomplish this:
class Form < ActiveRecord::Base
has_many :form_fields
has_many :form_values
end
class FormField < ActiveRecord::Base
belongs_to :form
has_many :form_values
end
class FormValue < ActiveRecord::Base
belongs_to :form_field
belongs_to :form
end
I'm sure there are other ways to do it. Document-oriented databases may provide other options.
Rails Forms for custom actions
I believe that using the check_path
helper that is generated from the routes file is your best bet.
The form should look like this then.
<%= form_tag(check_path) do %>
<%= text_field_tag(:idNumber) %>
<% end %>
Adding custom form into Devise edit page Ruby on Rails
Here your route user/edit/address is being generate from the match and is written via get method so you need to change it with
match 'user/edit/addresses' => 'registrations#addresses', via: :post, :as => :user_addresses
and in the form url path helper you are passing current user as a parameter while it does not exist in your route so form should get replace with
= form_for user_addresses_path, url: user_addresses_path, method: 'post', :html => { class: "form", :multipart => true } do |f|
Rails 3 Form For Custom Action
What if you did just :url => upload_photo_path(@photo)
?
It seems a little strange that you'd be uploading to a member though. Is this just a creation method (in which case you should just POST to the collection path)?
Related Topics
Marking an Unused Block Variable
Rails Way to Detect Mobile Device
How to Pass <Arguments> to Irb If I Don't Specify <Programfile>
Ending a Rails 2 Url with an Ip Address Causes Routing Error
How to Dynamically Call Accessor Methods in Ruby
Ruby: Eval with String Interpolation
In Ruby What's the Difference Between Self.Method and a Method Within Class << Self
Adding a "Like/Unlike" Button to a Post in Rails
Facebook Open Graph from Rails Heroku
Xpath Parent Attribute of Selection
Limiting Characters/Words in View - Ruby on Rails
How to Stop God from Leaving Stale Resque Worker Processes
Error Installing Gem: Couldn't Reserve Space for Cygwin's Heap, Win32 Error 487
Memory Usage Increase with Ruby 2.1 Versus Ruby 2.0 or 1.9
What's the Difference Between "=" & "=>" and "@Variable", "@@Variable" and ":Variable" in Ruby