Skip to content

Instantly share code, notes, and snippets.

@jakenotjacob
Created December 5, 2013 03:27
Show Gist options
  • Save jakenotjacob/7799671 to your computer and use it in GitHub Desktop.
Save jakenotjacob/7799671 to your computer and use it in GitHub Desktop.
R4iA - can't cast ActionDispatch::Http::UploadedFile to string
##Tickets Controller
class TicketsController < ApplicationController
before_action :require_signin! #except: [:show, :index] (temp-disabled)
before_action :set_project
before_action :set_ticket, only: [:show, :edit, :update, :destroy]
#Restrict Permissions (creating tickets)
before_action :authorize_create!, only: [:new, :create]
before_action :authorize_update!, only: [:edit, :update]
before_action :authorize_delete!, only: :destroy
def new
#build instantiates new record for Tickets assocation on Proj obj
@ticket = @project.tickets.build
3.times { @ticket.assets.build }
end
def create
@ticket = @project.tickets.build(ticket_params)
@ticket.user = current_user
if @ticket.save
flash[:notice] = "Ticket has been created."
redirect_to [@project, @ticket]
else
flash[:alert] = "Ticket has not been created."
render action: "new"
end
end
##(27...52)
private
def set_project
@project = Project.find(params[:project_id])
end
def set_ticket
@ticket = @project.tickets.find(params[:id])
end
def ticket_params
params.require(:ticket).permit(:title, :description,
assets_attributes: [:asset])
end
##SPEC
scenario "Creating a ticket with an attachment" do
fill_in "Title", with: "Add documentation for blink tag"
fill_in "Description", with: "The blink tag has a speed attribute"
attach_file "File #1", Rails.root.join("spec/fixtures/speed.txt")
attach_file "File #2", Rails.root.join("spec/fixtures/spin.txt")
attach_file "File #3", Rails.root.join("spec/fixtures/gradient.txt")
click_button "Create Ticket"
expect(page).to have_content("Ticket has been created.")
within("#ticket .assets") do
expect(page).to have_content("speed.txt")
expect(page).to have_content("spin.txt")
expect(page).to have_content("gradient.txt")
end
end
end
##Form Partial
<% number = 0 %>
<%= f.fields_for :assets do |asset| %>
<p>
<%= asset.label :asset, "File ##{number += 1}" %>
<%= asset.file_field :asset %>
</p>
<% end %>
<%= f.submit %>
<% end %>
#Model
class Ticket < ActiveRecord::Base
belongs_to :project
belongs_to :user
validates :title, presence: true
validates :description, presence: true, length: { minimum: 10 }
#Defines setter for file uploading
mount_uploader :asset, AssetUploader
has_many :assets
#=>Applies to Ticket's new/build/update-methods
accepts_nested_attributes_for :assets
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment