Simpleform association with images!
Hi Chris,
I'm wondering if you could help. I'm currently building a form that'll send out a SMS with twilio, and i'm looking to send details of a vehicle out at the same time, specifically the images of that vehicle. I have the form working perfectly except selecting which images are to be sent. If i run what i've put below I get 'can't write unknown attribute message_id
'
<%= simple_form_for [@vehicle, Message.new] do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :to %>
<%= f.input :company %>
<%= f.input :body %>
<%= f.association :vehicle_images,
:as => :check_boxes,
item_wrapper_tag: :div,
item_wrapper_class: "image-select",
:collection => @vehicle.vehicle_images.order(:id),
:label => false,
:inline_label => false,
:include_blank => false,
:input_html => {multiple: true},
:label_method => lambda { |b| image_tag(b.image.url(:thumb)) },
value_method: :id
%>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
def create
@vehicle = Vehicle.find(params[:vehicle_id])
@message = Message.new(message_params)
@message.vehicle_id = @vehicle.id
respond_to do |format|
if @message.save
@bucket = Bucket.new
@bucket.message_id = @message.id
@bucket.vehicle_id = @vehicle.id
@bucket.vehicle_images = params[:message][:vehicle_image_ids]
@bucket.save!
#byebug
format.html { redirect_to vehicle_messages_path(@vehicle), notice: 'Message was successfully created.' }
format.json { render :show, status: :created, location: @message }
else
format.html { render :new }
format.json { render json: @message.errors, status: :unprocessable_entity }
end
end
end
Is there anyting silly I'm missing from what you can see here? I'm rushing out but if you need more info to give any feedback, just let me know.
Thanks very much, it's appriciated!
Hey Martin,
Could you post the full error and a bit of the stacktrace as well? I'm guessing it has something to do with your associations.
I haven't seen the can't write unknown attribute
error message recently so I'm not quite sure what it refers to. Googling it leads me to believe maybe your Bucket doesn't have a message_id column or your associations are backwards maybe? https://stackoverflow.com/questions/20295710/activemodelmissingattributeerror-cant-write-unknown-attribute-ad-id-with-f
Thanks for taking a look Chris.
I do have message_id on Buckets.
class CreateBuckets < ActiveRecord::Migration
def change
create_table :buckets do |t|
t.integer :message_id
t.datetime :expires_at
t.integer :vehicle_id
t.integer :vehicle_images
t.timestamps null: false
end
end
end
Here is the various bits 'n bobs in gist
https://gist.github.com/MartinMcDermid/58c46474865fe25f95dd65f1485e5477
Thanks again!
Hmm, when I don't add any images I get 'VehicleImage(#70203987369520) expected, got String(#47143662444640)'
It was just me being an idiot Chris, I didn't create the join table for messages and vehicle_images. Schoolboy error.
Thanks again.
Doh. I looked at your code and didn't have any good answers. Didn't cross my mind that the table might be missing. 😜
It's been a long week! :) Thanks for taking the time anyway, it's very much appriciated!
You also have a weird ability to create videos which contain stuff I'm working with at the time! You think you could point me in the right direction with this? https://stackoverflow.com/questions/44664847/using-impressionist-with-chartkick-displaying-user-articles-impressions-in-gra