Ask A Question

Notifications

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, 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!

Reply

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

Reply

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!

Reply

Hmm, when I don't add any images I get 'VehicleImage(#70203987369520) expected, got String(#47143662444640)'

Reply

It was just me being an idiot Chris, I didn't create the join table for messages and vehicle_images. Schoolboy error.

Thanks again.

Reply

Doh. I looked at your code and didn't have any good answers. Didn't cross my mind that the table might be missing. 😜

Reply

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

Reply
Join the discussion
Create an account Log in

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

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

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