Rails Validation Error not Appearing in Simple_Form - ruby-on-rails-4

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.

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

dynamic fields in redmine based upon input

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

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

Rails Cocoon Delete/Destroy Single Instance

I am using Cocoon gem to associate resume fields to one resume, and each user has ONE resume and have everything working the correct way via cocoon documentation. However, I would like the ability for users to be able to edit resume fields/nested models directly on the profile page without being redirected to the cocoon nested form page. Further more I would like users to have the ability to add and or delete a SINGLE field/entry. Is there any way at all to accomplish this?
In my console I have been able to successfully delete an entire resume nested model using
User.last.resume.resume_edus.destroy_all
Where 'resume.edus' is the nested model inside of resume complete with 3 other 'text_fields'. But as stated I would only like to be able to edit/delete a single instance of resume_edus. Any ideas?
User.rb
has_one :profile
has_one :resume
Resume.rb
belongs_to :user
has_many :resume_edus
accepts_nested_attributes_for :resume_edus,
reject_if: :all_blank,
allow_destroy: true
Resume_edu
belongs_to :resume
Resume Controller
params.require(:resume).permit(:user_id, :cover,
resume_edus_attributes:
[:id, :title, :date, :description, :_destroy])
Figured out the answer! I passed the resume_edus 'ID' in the delete action
<%= link_to "Delete", resume_path(r.id), method: :delete %>
Then was able to search for said ID in the current users resume_edu modal to specifically isolate and delete it without deleting the other instances of the model!
def destroy
#instance = current_user.resume.resume_edus.find(params[:id])
debugger
#instance.destroy
flash[:notice] = "Resume Field Was Deleted"
redirect_to profile_path(current_user)
end
accepts_nested_attributes_for :order_line_items, allow_destroy: true
<tr class="nested-fields">
<%= f.hidden_field :_destroy %>
<%= link_to_remove_association "Delete", f %>
</tr>
Add _destroy in controller params:
order_line_items_attributes: %i[id product_id quantity price discount total _destroy]

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 %>