I have a form with four input fields where a user creates an item
. In the form one of the four input fields is for picture which is used to create a user_item
at the same time on form submission. I am running into several problems.
When validation fails on the
item
I have the controllerrender :new
but when this happens the picture input field is not visible.Validation on presence of picture is not working.
I need a way to set the
user_id
attribute tocurrent_user
on theuser_item
when it is created.
item.rb
validates :name, presence: true, uniqueness: true
validates :description, presence: true
has_many :tags
has_many :user_items
has_many :users, -> { uniq }, through: :user_items
belongs_to :user
accepts_nested_attributes_for :user_items
validates_associated :user_items
user_item.rb
belongs_to :user
belongs_to :item
mount_uploader :picture, PictureUploader
validates_presence_of :picture
validate :picture_size
items_controller.rb
def new
@item = Item.new
@item.user_items.build
end
def create
@item = item.new item_params
if @item.save
redirect_to items_path, notice: "Thank you for your item request!"
else
render :new
end
end
private
def item_params
params.require(:item).permit(:name, :description, :tag_list, user_items_attributes: [:picture]).merge(created_by: current_user.id)
end
new.html.erb
<%= simple_form_for @item, html: { class: "create-item-form" } do |item_builder| %>
<div class="well">
<%= item_builder.input :name, required: false, error: false, label: "Item name" %>
<%= item_builder.input :description, as: :text, required: false, error: false, label: "Description of item" %>
<%= item_builder.input :tag_list, required: false, label: "Tags (these will help users find your item)" %>
<%= item_builder.simple_fields_for :user_items do |user_item_builder| %>
<%= user_item_builder.input :picture, as: :file, required: false, label: "Picture of you with this item" %>
<% end %>
</div>
<div class="clearfix">
<%= item_builder.submit 'Submit new item request', class: "btn btn-primary pull-right inherit-width" %>
</div>
<% end %>
The simplest way to add the
user_id
to theuser_item
is to include ahidden_field
in yourfields_for
. Not the most secure, but should work:According to this answer:
Nested Input Disappears When Form Reloads
, you need to rebuild yourpicture
objects:file_field
inputs are particularly interesting. Because your OS cannot guarantee your files will be exactly the same as they were, so thefile_field
is not populated.You should use
inverse_of
to make sure the two objects can talk to each other:Update
If you wanted to pass the
user_id
through the backend, not having a hidden field, you'd be able to do something like this:This would have to be accompanied with the following change to your
items#new
view:You'll also need to pass the attributes through the respective models: