I have a model named Form, which get fields as per user requirement, eg if user puts text field in the form then a attribute is created in Form model for storing string data.
Similarly I want to store date/time and datetime values. So I added
include Mongoid::MultiParameterAttributes
in the form model, because date and time values are submitted from for in multiple attributes.
But I get Mongoid::MultiParameterAttributes::Errors::MultiparameterAssignmentErrors exception in the controller create action, on the line #form = Form.new(params[:form])
def create
#form = Form.new(params[:form])
if #form.save
redirect_to(form_path(#form))
else
redirect_to :action => "new"
end
end
How do I get through this.
Please Help
Modify your Form class so it looks like the one below.
class Form
include Mongoid::Document
include Mongoid::MultiParameterAttributes
...
end
The ruby driver can only serialize Time objects. That might be your problem.
Related
I'm writing an ActiveAdmin form for a record which takes an Array of Strings. Let's call them widgets. I'm using a multi select form like so:
ActiveAdmin.register Things do
permit_params widgets[]
form do |f|
f.inputs do
...
f.input :widgets,
as: :select,
collection: [],
multiple: true
end
end
end
With a bit of Javascript to help the user can enter their widgets and it will be delivered as an Array of Strings just fine. But when editing an existing record the existing record.widgets are not filled into the field, the widgets field is blank. I suspect what's happening is ActiveAdmin/Formtastic is interpreting this select as a has_many relationship with Widget, and the Strings as Widget IDs. ActiveAdmin is ignoring the failure.
How can I write an input to pass a plain Array of Strings into a model with ActiveAdmin? I'd like my parameters as params[:thing][:widgets] = ["foo", "bar"]
By providing collection: [], you are telling the form to always start with blank array.
You need to provide the actual Thing's widgets to the form. You'll need to modify the example to play well with the select box / javascript you wrote - just bear in mind that collection takes an array - collection: resource.widgets.split(',').
I am using a simpleform collection association input, as checkboxes in order to allow users to choose 1+ items from a list as part of an order. The list has a has_and_belongs_to_many association with the overall order. I want them to be able to do multiple of the same items, however. So I would want a small number input next to each checkbox. I can handle the javascript, I am just wondering how to do this with simpleform, if its even possible.
Thanks!
It is possible if you write a custom input for this specific task.
You would need to put it in # app/inputs/your_input.rb for Simple_form to automatically pick it up.
Then in the file :
class YourInput < SimpleForm::Inputs::Base
def input
# Your code here, but I just pasted the example that adds something after the existing field already as a reference.
"$ #{#builder.text_field(attribute_name, input_html_options)}".html_safe
end
end
and in the form :
f.input :money, as: :your
In case of checkbox, the method name will change to "check_boxes" from "input"
I want to update an specific attribut in my database after save data.
For example i have an column in my table which called pending and it's an boolean. i want to set this value to true when data was saved.
after_save :do_something
private
def do_something
self.update_column(:pending, true)
end
This doesn't work. Anyone here who has an solution?
You can modify your attribute directly like:
after_save { |user| user.username = user.username.downcase }
Try after_commit instead of after_save. It will operate outside of the save transaction.
The Rails3 form_for view helper displays current values on edit.
I would like to populate the model with some initial values on create. Not the same initial value every time, but actually copy some of the values from the last record stored.
When I do this, and use the form_for helper to construct the form, none of the values appear.
It looks like form_for does not emit the value attributes for input fields of a model not yet saved? But I wouldn't save the newly created model before displaying the form because: 1. it would not validate, 2. that complicates the semantics of canceling, 3. navigating away from the form would leave an unintentionally saved record.
How can I get a form rendered from the new action to display some dynamic defaults?
Seriously. I've been reading all day without an answer. This seems like there ought to be an easy solution that I'm missing.
The problem came from the initialization code in the model. The controller had
def new
#post = Post.new
#post.initFromLast
end
The model had
def initFromLast
last_post = Post.last
title = last_post.title
summary = last_post.summary
end
The model needed
def initFromLast
last_post = Post.last
write_attribute(:title, last_post.title)
write_attribute(:summary, last_post.summary)
end
Ruby apparently interpreted the first form as assignments to local variables.
Try something like this in your controller:
def new
last_post = Post.last
#post = Post.new
#post.title = last_post.title
#post.summary = last_post.summary
...
end
... then in your view you should have something like:
form_for #post do |form|
form.text_field :title
form.text_area :summary
...
The form should have the initial values even if the record is unsaved.
I'd like to slugify the urls for a model in Rails 3, using Mongoid. The problem is the fields I want to use in the slug are located in a child model. I am using mongoid-slug gem to find a solution to this and my attempt so far is this:
class Building
references_one :address
def to_param
address.to_param
end
end
class Address
referenced_in :building
field :houseno
field :street
slug :houseno, :street
end
While this allows me to form the correct url by calling building_path(building), the page does not contain the correct values. Error message complains that object id is incorrect, and I'm not sure how to get Rails to listen and find the record by to_param.
For the curious, here is how I solved my own problem. I realized that I needed to use change the show action from
#building = Building.find(params[:id])
to
#building = Address.find_by_slug(params[:id]).building
And voila! It works.