Model has accepts_nested_attributes_for for the relation and the form is as follows:
= semantic_form_for #obj, :url => path do |f|
= f.inputs do
= f.input :name
= f.semantic_fields_for :photos do |p|
= p.inputs :desc
= f.buttons
The form works well and everything is fine. However, I would like to display each photo near the field, so the user could se which picture she is going to annotate. Is there anyway, to access photo.id inside the fields_for loop?
Edit:
Is there any way to alter the order in which photos fields will be rendered in this example. As far as I can tell till now, that order is pretty random.
Well this was easy enough, the object is accesible by:
p.object
:-)
Second part of the question is solved with:
default_scope order(...)
Related
I am trying to use the Cocoon gem to add/remove assets on asset_profiles. Everything up to this point works, I am just having a problem with link_to_remove_association. When the button is clicked, the field is removed. But if the field had been filled in and saved previously, I am unable to remove said association. The button just hides the field until I hit update. Is there a way to actually remove the association from the database through link_to_remove_association? Any help is much appreciated. Thanks in advance.
Here is the code I am referring to:
Asset.show
= simple_form_for([#asset_profile, #asset]) do |f|
= f.input :max_users, as: :hidden
#assets_users
= f.simple_fields_for :assets_users do |assets_user|
= render "assets_user_fields", f: assets_user
.links
= link_to_add_association "Add Another User", f, :assets_users
= f.submit
Asset._assets_users_fields
.nested-fields
= f.input :user_id, collection: #users.order(:last_name), :label => "User"
= link_to_remove_association "Remove", f
Screenshot of page pre-remove:
Screenshot of post-remove:
Screenshot of post-update(page reload):
I would much rather, after the update, the page be reloaded and looking like below, which is the form for initially adding users to an asset:
When using strong parameters, you have to make sure to permit :id and :_destroy, as documented by the way (see documentation ).
It is not unlogical: cocoon sets the _destroy if something needs to be removed, rails then needs the id to know what to remove.
Im my case, i forget for allow_destroy: true.
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
Did you permit the nested parameter _destroy in either permit (strong_parameters) or attr_acceptible?
Your asset_profile_params should look like
def asset_profile_params
params.require(:asset_profile).permit(:max_users, :asset_users_attributes => {:user_id, :_destroy})
end
I have a Message data model in Rails with a "virtual" association. That is, a method that returns a collection of "associated objects", but it is not an actual association in the ActiveRecord sense.
class Message
def recipients
#recipients
end
def recipients=(arr)
#recipients = arr
end
end
The thing with simple_form is that, when I try to show a association field, it fails because there's no :recipients association:
= simple_form_for(#message) do |f|
= f.association :recipients, collection: Recipient.all
The solution seemed then simple, I went on to use a f.input with the as: :select option:
= simple_form_for(#message) do |f|
= f.input :recipients, as: :select, collection: Recipient.all
And this works fine, except for the fact that it does not automatically detect the values already in #message.recipients so that the elements appear pre-selected when the form is rendered.
If Message#recipients were an actual association, in the ActiveRecord sense, then f.association in the form would do this as well. But for reasons that go beyond the scope of this question, I can't make it as an actual association.
The question then is, can I achieve f.input :recipients, as: :select to pre-select the selected elements?
Well, this is embarrassing. Just after posting this, I came up with an idea that ended up solving the problem:
class Message
# ...
def recipient_ids
# ...
end
def recipient_ids=(arr)
# ...
end
end
I added the recipient_ids getter and setter, in addition to the association getter and setter that I had before.
Then I updated the form input field to refer to :recipient_ids instead of :recipients:
= simple_form_for(#message) do |f|
= f.input :recipient_ids, as: :select, collection: Recipient.all
I am quit new to rails. I have been using formtastic in my project and I find it quite easy to deal with the form objects. I have a small problem which I hope to clear out here.
I want to create a form for arbitrary objects and a has_many type of nested form for it. What I mean is semantic_form_for does not use any model instead uses symbol for creating form and this form now has to have a to_many type of semantic_fields_for. This is how my code looks,
= semantic_form_for :company do |f|
= f.inputs "company" do
= f.input :name
= f.input :enterprise_code
= f.semantic_fields_for :email do |e|
= f.inputs "email" do
= f.input :address
The form above is not associated to any model. I will pick these attributes in the controller and assign it individually. The email fields in the form has to be like has_many. Now, it is like one to one. How can this be achieved.
I used formtastic only once so i might be wrong but i don't think there's anything mentioned that you can use it only for one type of association. It only says that you for nested forms we can use semantic_fields_for :model and then both model's must be setup correctly with accepts_nested_attributes_for
I have two models User and Category that have a HABTM association.
I would like to generate checkboxes from a collection of Category items on my view, and have them associated with the current_user.
How do I do that?
Thanks.
P.S. I know that I can do the equivalent for a dropdown menu with options_from_collection_for_select. I also know that Rails has a checkbox_tag helper. But not quite sure how to do both of them. I know I can just do it manually with an each loop or something, but am wondering if there is something native to Rails 3 that I am missing.
Did you check out formtastic or simple_form
They have helpers to write your forms more easily, also to handle simple associations.
E.g. in simple_form you can just write
= simple_form_for #user do
= f.association :categories, :as => :check_boxes
In form_tastic you would write
= simple_form_for #user do
= f.input :categories, :as => :check_boxes
Hope this helps.
You can use a collection_select and feed it the options. Assuming you have a form builder wrapped around a user instance, you can do something like this:
form_for current_user do |f|
f.collection_select(
:category_ids, # the param key, so params[:user][:category_ids]
f.object.categories, # the collection of items in the list
:id, # option value
:name # option string
)
end
You may want to pass the :multiple => true option on the end if needed.
I am still struggling both writing the controller and the actual form to be able to nest one form in another with an optional model?
I have Message which has many contacts
When submitting a message, I want to add a contact optionally.
I have this as an example:
= simple_form_for Message.new, :remote => true do |f|
#message_form
= f.error_messages
%p
= f.input :account_name, :url => autocomplete_account_name_messages_path, :size => 40, :as => :autocomplete
%p
= f.input :topic, :required => true,
:input_html => {:size => 30}
#add_contact_btn
= link_to "Add Contact"
#contact_form
= f.simple_fields_for :contactd do |fc|
= fc.input :email
= fc.input :first_name
= fc.input :last_name
= f.submit 'Give'
= f.submit 'Request'
For Message.rb model, I have the following:
has_many :contacts
accepts_nested_attributes_for :contacts, :reject_if =>:all_blank
Note: When I used :contacts in the simple_fields_for it didn't work, so it is singular. But the reverse for accepts_nested_attributess_for.
In my create controller for message, I included message.contacts.build
But right now I am still generating nil contacts.
Here is what I see passed as form data from google chrome:
message%5Baccount_name%5D:McKesson
message%5Btopic%5D:testing a contact
message%5Bbody%5D:testing this
sender_id:
receiver_id:23
message%5Bcontacts%5D%5Bemail%5D:888#gmail.com
message%5Bcontacts%5D%5Bfirst_name%5D:Ang
message%5Bcontacts%5D%5Blast_name%5D:Name
The correct method name is simple_fields_for (notice the plural)
Also, you need to keep the f. to call it on the simple_form object
I have a small project where I demonstrate how to use nested forms in simple-form, combined with cocoon (a gem I created to add/remove nested elements dynamically).
The project is on github.
Hope this helps.
In my create controller for message, I included message.contacts.build
But right now I am still generating nil contacts.
Make sure you put in your Message.rb model the ability for it to accept the attributes too.
class Message < ActiveRecord::Base
attr_accessible :contacts_attributes
has_many :contacts
accepts_nested_attributes_for :contacts
I know it doesn't answer your question fully but it may have just been this. When it comes to my project, it would return nil if i didn't include the :contacts_attributes, in my case it deals with products. Hope this helps even if I'm not using simple form as of now!
I faced similar issues working with nested forms. As suggested by JustinRoR you need to define
attr_accessible: contacts_attributes.
You should be able to test the hash in the ruby console ( I am not sure if you have tried this). I suggest you to print the params[:message] and use this to create message from console like Message.new(params[:message]). (Note params[:message] is what you get by printing the params[:message] hash).
Once it works in console it should work like charm