dynamic fields in redmine based upon input - redmine

I am using custom fields in redmine. I need a set of custom fields to populate based upon how a user answers a question. for instance, if a user chooses "a" they get a series of 3 custom fields that pertain to "a" ..if a user chooses "b" they get a series of custom fields that pertain to "b" is this possible? any help would be great!

Are You search ready plugin or want to develop your own with neccesary functionality?
In case of develop You may pass selected field from view to controller as parameter. Then check which field was selected and set value to it from other parameter.
I think that somethng like this:
view
<%= form_tag ... do %>
<%= label_tag :selected_field %>
<% select_tag :selected_field, options_for_select(['field1', 'field2',...])
<%= label_tag :value %>
<% text_box_tag :value, value %>
<%= submit_tag 'save' %>
<% end %>
and controller method
def update
obj = SomeClass.find_by... # get your instance
case params[:selected_field]
when 'field1'
obj.field1 = params[:value]
when 'field2'
obj.field2 = params[:value]
end
obj.save
end

Related

Rails unedited form fields if db column is NULL

Let me try to present a simple example here:
I have db table Orders and a column delivery_address.
<%= form_for #order do |f| %>
<%= f.text_field :delivery_address %>
<% end %>
If no change is made on the form, when the form is submitted the value of Orders.delivery_address changes from NULL to empty value.
and I set a alert notice which looks like:
test#gmail.com changed delivery_address to .
Any suggestion how to prevent updating db fields with NULL values to empty by default with rails update action.
You could do something like this in the model:
# In the Order model
before_validation do
self.delivery_address = nil if delivery_address.blank?
end
I also really don't like that this happens, but the other alternative is to do it on the controller level

Rails 4.2+: Mapping PostgreSQL JSONB nested values to form fields

I have a users table with a settings field of type JSONB (using PostgreSQL 9.5).
I'm trying to create a form on a settings page to update user.settings["notifications"][...] values.
class User < ActiveRecord::Base
end
user = User.create(settings: { notifications: { post_created: true } })
user.settings["notifications"]["post_created"] # => true
To map the nested JSONB values to a form, however, I have to do this:
# views/form.html.erb
<input type="check" name="user[settings][notifications][post_created]" checked="<%= current_user.settings['notifications']['post_created']" %>
class SettingsController
def update
current_user.settings["notifications"]["post_created"] = params["user"]["settings"]["notifications"]["post_created"]
current_user.save
end
end
Is there anyway to utilize the power of Rails form builders such that I can do:
# will not work, undefined attribute settings['notifications']['post_created']...
<%= form_for current_user, url: settings_path, method: "PUT" do |f| %>
<%= f.check_box "settings['notifications']['post_created']" %>
<% end %>
I understand that Rails is trying to map an attribute from the current_user object, and there isn't really an "attribute" named settings['notifications']['post_created'].
But how does one go about mapping nested JSONB values to a form field for CRUD activity?
A workable (but not really feasible) approach is to created virtual attributes for every single nested value I want to work with:
class User
def settings_notifications_post_created
settings["notifications"]["post_created"]
end
def settings_notifications_post_created=(value)
settings["notifications"]["post_created"] = value
end
end
# view...
<%= f.check_box :settings_notifications_post_created %>
But this loses any benefit of a conventional system since I'm manually typing out every attribute. May as well write raw HTML fields and all the getter/setter methods myself...Googling and Stack Overflow haven't been very helpful so far, it seems there aren't very many with experience doing this kind of stuff yet...

Rails Validation Error not Appearing in Simple_Form

I have a validation in my Author model that checks if the current User already is associated with an Author record before creating a new Author. While the validation is triggering, the error message isn't displaying in the form.
author.rb
belongs_to :user
validates :user, :uniqueness => {:message=>"An author account already exists for this user"}
author#new
<%= simple_form_for(#author) do |f| %>
<%= f.error_notification %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :bio %>
<%= f.button :submit %>
<% end %>
What happens is that it shows that there's an error, but no message appears. How can I fix this? Thank you!
simple_form only shows errors for input fields that have errors. You have an error on the attribute "user" but you have no input field for that where the error could be shown. You only have first name, last name and bio.
In your Controller you might want to redirect to the existing author instead when there already is an author. Or simply show #author.errors.full_messages to tell the end user what's going on.

Selecting Associated records with Rails and simple form

I have a project with partials associated with it. I am trying to create a selection box so that the user can select a parcel from the associated parcels to add to another model associated with the project. What I have below is my attempt that displays the parcels but simply returns the id ie I get
undefined method `each' for "64":String
With 64 being the parcel's ID.
I would also like to allow the user to select multiple parcels or no parcels.
<% #pro_par = #project.parcels %>
<%= f.input :parcels, :collection => #pro_par, :label_method =>:tax_parcel %>
How can I fixe this?
If you already have a associated model like
class Article < ActiveRecord::Base
has_many :parcels
# rest of the code
end
then you can directly call the associating models in the view using simple form
<%= simple_form_for #project do |f| %>
<!-- remaining codes -->
<%= f.association :parcels %>
<%= f.button :submit %>
<% end %>
it automatically lets you select multiple parcels.
Reference: https://github.com/plataformatec/simple_form#associations

Mixing model-bound and non-model-bound fields in Rails form

I have a form in my Rails app that passes its data to a third party (Stripe) without a model. However, my client now wants the same form to include other fields that are persisted. How do I create a form that submits model bound and non-model-bound fields?
I'm not familiar with Stripe, but you can easily include non-database-bound attributes in the model using 'attr_accessor'.
in your model
class MyModel < ActiveRecord::Base
attr_accessor :stripe_attribute_one, :stripe_attribute_two
end
in your form
# assuming your model has migrated attributes called :db_attribute_one and :db_attribute_two
<%= form_for #my_model %>
<%= f.text_field :db_attribute_one %>
<%= f.text_field :db_attribute_two %>
<%= f.text_field :stripe_attribute_one %>
<%= f.text_field :stripe_attribute_two %>
<% end %>