Selecting Associated records with Rails and simple form - ruby-on-rails-4

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

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

Rails 4 - Update Join Table Attributes with has_many through

This is a follow-up of my question: Rails 4 - Access Join Table Value in views
Now I know how to retrieve join table attributes and show them in view. But still I can't find the right approach to edit and update the attribute.
Models:
class Recipe < ActiveRecord::Base
has_many :recipe_ingredients
has_many :ingredients, through: :recipe_ingredients
end
class Ingredient < ActiveRecord::Base
has_many :recipe_ingredients
has_many :recipes, through: :recipe_ingredients
end
class RecipeIngredient < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient
#there is an attribute t.text :amount
end
Recipe#Edit:
...
<%= simple_form_for #recipe do |f| %>
<div class="form-group">
<%= f.input :name, input_html: { class: "form-control"} %>
<%= f.input :description, input_html: { class: "form-control"} %>
<%= f.collection_select(:ingredient_ids, Ingredient.all, :id, :title, {}, {multiple: true}) %>
<!-- reserve for recipe_ingredients.amount -->
</div>
<%= f.submit "Submit", disable_with: 'Submitting...', class: "btn btn-primary"%>
<% end %>
...
As shown above, this is a many-to-many relationship, each recipe may have several ingredients. It works fine and I can choose the right models to associate with (using collection_select). But I have left the work to edit and update join table attributes undone. I have two ideas in my mind:
Make another edit page for editing join model attributes
Ajax? But not quite familiar with
I know the question seems to be trivial, but many solutions are out-dated(Rails 3). Desperate for a Rails 4 solution.
Edit
Intention: I would like to make the creation of Recipe and Ingredient models on their own, which is done. User will see which recipe using the ingredient in Ingredient#show action, which is done also. As to Recipe#show action, there is a slight difference, where user will see which ingredient it is using as well as the amount(join table attribute), which is done in my previous question. The last problem is, I would like to make this attribute editable.
You may want to look into doing nested models combined with the cocoon gem. This will allow you to have a form which updates multiple models at once and it takes care of all the ajax for you. https://github.com/nathanvda/cocoon. I used this gem on a system where the user needed to be able to add one or more answers to a trivia question via dynamic form fields. I think it could work for you as well with giving the user the ability to add one or many RecipeIngredient relations for a recipe. You would be able to add selects dynamically to your form via Cocoon containing a select with the Ingredient model records.

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

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