Ask A Question


You’re not receiving notifications from this thread.

Simpleform association with images!

Martin McDermid asked in Rails

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,] 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 class="form-actions">
    <%= f.button :submit %>
<% end %>

  def create
    @vehicle = Vehicle.find(params[:vehicle_id])
    @message =
    @message.vehicle_id =

    respond_to do |format|

        @bucket =
        @bucket.message_id =
        @bucket.vehicle_id =
        @bucket.vehicle_images = params[:message][:vehicle_image_ids]!


        format.html { redirect_to vehicle_messages_path(@vehicle), notice: 'Message was successfully created.' }
        format.json { render :show, status: :created, location: @message }
        format.html { render :new }
        format.json { render json: @message.errors, status: :unprocessable_entity }

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?


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

Here is the various bits 'n bobs in gist

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?

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 83,168+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.