I'm using carrierwave to upload files/images to my application. I'm storing the files through Amazon AWS. The upload works great for images but when trying to add .PDF's I'm receiving a "rollback transaction" error in my server logs and I'm not sure why.
Started POST "/restaurants" for 127.0.0.1 at 2013-06-21 14:39:16 -0400
Processing by RestaurantsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"filAk5Jh++qmb7owXVgYin2C4nq2O+09gMeQV27gKeU=", "restaurant"=>{"name"=>"The Stumble Inn", "address"=>"", "phone"=>"", "description"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0x00000101da3f98 #original_filename="stumble.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"restaurant[image]\"; filename=\"stumble.jpg\"\r\nContent-Type: image/jpeg\r\n", #tempfile=#<File:/var/folders/6b/tq59gs0d1f7bp_zg41cf6fqm0000gn/T/RackMultipart20130621-40432-124zf2w>>, "remote_image_url"=>"", "menu"=>#<ActionDispatch::Http::UploadedFile:0x00000101da3d18 #original_filename="menu.pdf", #content_type="application/pdf", #headers="Content-Disposition: form-data; name=\"restaurant[menu]\"; filename=\"menu.pdf\"\r\nContent-Type: application/pdf\r\n", #tempfile=#<File:/var/folders/6b/tq59gs0d1f7bp_zg41cf6fqm0000gn/T/RackMultipart20130621-40432-3cwvjc>>}, "commit"=>"Create Restaurant"}
(0.2ms) begin transaction
(0.2ms) rollback transaction
Redirected to http://localhost:3000/restaurants
class MenuUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
# Choose what kind of storage to use for this uploader:
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
class Restaurant < ActiveRecord::Base
attr_accessible :address, :description, :name, :phone, :image, :remote_image_url, :menu
mount_uploader :image, ImageUploader
mount_uploader :menu, ImageUploader
end
new.html.erb
<h1>Add new restaurant</h1>
<%= form_for(#restaurant, :html => {:multipart => true}) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :address %>
<%= f.text_field :address %>
<%= f.label :phone %>
<%= f.text_field :phone %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.label :upload_image %>
<%= f.file_field :image %>
<%= f.label :remote_image_url, "or image URL:" %>
<%= f.text_field :remote_image_url %>
<%= f.label :upload_menu %>
<%= f.file_field :menu %><br/><br/>
<%= f.submit "Create Restaurant" %>
<% end %>
class RestaurantsController < ApplicationController
def new
#restaurant = Restaurant.new
end
def create
#restaurant = Restaurant.new(params[:restaurant])
#restaurant.save
redirect_to restaurants_path
end
I realized that I had a type in the Restaurant model.
mount_uploader :menu, ImageUploader
should be:
mount_uploader :menu, MenuUploader
Related
I'm following along Railscasts #250 Authentication from Scratch but have an issue where the Password Confirmation can be different from the Password and the user will still be created.
..model/dealer.rb:
class Dealer < ActiveRecord::Base
attr_accessor :password
before_save :encrypt_password
validates_confirmation_of :password
validates_presence_of :password, :on => :create
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
end
..controllers/dealers_controller.rb
class DealersController < ApplicationController
def new
#dealer = Dealer.new
end
def create
#dealer = Dealer.new(dealer_params)
if #dealer.save
redirect_to root_url, :notice => "Signed Up!"
else
render "new"
end
end
private
def dealer_params
params.require(:dealer).permit(:email, :password)
end
end
..views/dealers/new.html/erb
<%= form_for #dealer do |f| %>
<p>
<%= f.label :email %><br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br>
<%= f.password_field :password_confirmation %>
</p>
<p class="button"><%= f.submit %></p>
Any ideas what I need to do for this to work? Two people in the comments of the Railscast had the same issue but no answer.
Have you tried to add password_confirmation to your allowed params like this:
def dealer_params
params.require(:dealer).permit(:email, :password, :password_confirmation)
end
If this doesn't help try to generate accessor for password_confirmation too:
attr_accessor :password, :password_confirmation
When I create a new comment the text saves fine but the commenter is not being saved. I have verified that the column for commenter exists and that the parameter is being passed. The field just isn't inserting into the table and I have no clue why.
Form:
<%= form_for #comment do |f| %>
<p>
<%= f.label :new_comment %>
<%= f.text_field :text %>
<%= hidden_field_tag :user_id, params[:user_id] %>
<%= hidden_field_tag :post_id, params[:id] %>
<%= f.submit "Comment" %>
</p>
<% end %>
Action:
def create
#user = User.find(params[:user_id])
#post = #user.posts.find(params[:post_id])
#comment = #post.comments.create(params[:comment].permit(:text, :commenter))
redirect_to show_post_path(#user, #post)
end
Add one more field in form which will pass commenter for comment
<%= form_for #comment do |f| %>
<p>
<%= f.label :new_comment %>
<%= f.text_field :text %>
<%= hidden_field_tag :user_id, params[:user_id] %>
<%= hidden_field_tag :post_id, params[:id] %>
<%= hidden_field_tag 'comment[commenter]', params[:user_id] %>
<%= f.submit "Comment" %>
</p>
<% end %>
OR
Change controller code to
def create
#user = User.find(params[:user_id])
#post = #user.posts.find(params[:post_id])
#comment = #post.comments.new(params[:comment].permit(:text, :commenter))
#comment.commenter = #user.id
#comment.save
redirect_to show_post_path(#user, #post)
end
I'm guessing you want your commenter_id in your form instead of user_id. Change your user_id line in your form to:
<%= hidden_field_tag :commenter_id, params[:commenter_id] %>
And then change the third line in your create function:
#comment = #post.comments.create(params[:comment].permit(:text, :commenter_id))
Have you check your association between User model and Comment Model?
in User model it should be has_many :comments and in Comment Model belongs_to :user
Firstly this is the first time I have ever tried to use accepts_nested_attributes_for, so I am still learning and hopefully I have just missed something small.
So I have setup and follow an example for doing accepts_nested_attributes_for but I keep getting a ActiveModel::MassAssignmentSecurity::Error at /reports Can't mass-assign protected attributes: deployment
and I can't figure out why, any help would be grateful code is below:
Report Model:
class Report < ActiveRecord::Base
attr_accessible :name, :weekending, :visable, :deployments_attributes
has_many :deployments, dependent: :destroy
accepts_nested_attributes_for :deployments, allow_destroy: true
end
Deployments Model:
class Deployment < ActiveRecord::Base
attr_accessible :deployment_comments, :deployment_name, :environment, :report_id
belongs_to :report
end
report/new.html.erb
<%= form_for (#report) do |f| %>
<%= render 'shared/error_messages' %>
<h4>Reprot Detials</h4>
<%= f.label "Report Written By: " %> <%= f.text_field :name %><br>
<%= f.label "Weekending: " %> <%= f.text_field :weekending %><br>
<%= f.label "Visable: " %> <%= f.text_field :visable %><br>
<%= f.fields_for :deployment do |builder| %>
<%= render 'reports/forms/deployment_fields', f: builder %>
<% end %><br><br>
<%= f.submit class: "btn btn-large btn-primary" %>
<% end %>
report/forms/_deployment_fields.html.erb
<h4>Deployments</h4>
<%= f.label :deployment_name, "Name of Deployment:" %><br />
<%= f.text_field :deployment_name %><br />
<%= f.label :environment, "Environment of Deployment:" %><br />
<%= f.text_field :environment %><br />
<%= f.label :deployment_comments, "Deployment Comments:" %><br />
<%= f.text_area :deployment_comments %><br />
Reports Controller:
class ReportsController < ApplicationController
def index
#reports = Report.paginate(:page => params[:page],
:per_page => 20,
:order => 'Weekending desc')
end
def show
#report = Report.find(params[:id])
end
def new
#report = Report.new
end
def create
#report = Report.new(params[:report])
if #report.save
redirect_to reports_url
else
render 'new'
end
end
DB Migrations:
timestamp_create_deployments.rb
class CreateDeployments < ActiveRecord::Migration
def change
create_table :deployments do |t|
t.string :environment
t.integer :report_id
t.string :deployment_name
t.string :deployment_comments
t.timestamps
end
add_index :deployments, [:report_id]
end
end
timestap_create_reports.rb
class CreateReports < ActiveRecord::Migration
def change
create_table :reports do |t|
t.date :weekending, :null => false
t.string :name, :null => false
t.boolean :visable, :default => false, :null => false
t.timestamps
end
end
end
So figured it out, once I changed
<%= f.fields_for :deployment do |builder| %>
to
<%= f.fields_for :deployments do |builder| %>
I found that the deployments fields didnt show, so then I added
#report.deployments.build
to my new method of my report controller!
I'll just need to figure out if I need to do anything to the update/edit method or not?
I've problem with my "subscriber". It should just add an email to db with symptom active/inactive. It gaves me this error - undefined method `[]' for nil:NilClass
Extracted source (around line #2):
1: <div id="subscriber">
**2: <%= form_for #subscriber do |f| %>**
3: <%= f.label :email %>
4: <%= f.text_field :email %>
5: <%= f.hidden_field :active, :value => true %>
Here is model:
class Subscriber < ActiveRecord::Base
attr_accessor :email, :active
validates_format_of :email, :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
This is my view:
<div id="subscriber">
<%= form_for #subscriber do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.hidden_field :active, :value => true %>
<%= f.submit t('.submit') %>
<% end %>
</div>
Here is controller:
class SubscribersController < ApplicationController
def create
#subscriber = Subscriber.new(params[:subscriber])
if #subscriber.valid?
flash[:notice] = "Thx for subscription"
#subscriber.save
redirect_to root_url
else
render :action => 'new'
end
end
end
Here are routes:
resources :subscribers
I'm new with RoR, so if you need some more logs or info let me know. Thx alot.
I have three models, and i am trying to save onto a third table using simple form gem and checkboxes.
class Work < ActiveRecord::Base
has_many :skills, through: :skillships
end
The second model is
class Skill < ActiveRecord::Base
has_many :works, through: :skillships
end
The third is
class Skillship < ActiveRecord::Base
belongs_to :work
belongs_to :skill
end
Using the Work model i am trying to save the data on the skillship table. Something similar to this http://railscasts.com/episodes/17-habtm-checkboxes-revised. Can you please help.
EDIT
The form
<%= simple_form_for(#work) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title, :label => 'Project Title' %>
<%= f.input :excerpt, :as => :text %>
<fieldset>
<legend>Skills Used </legend>
Would like to check the skills i used here.
</fieldset>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
I tried..
<%= hidden_field_tag "work[skill_ids][]", nil %>
<% Skill.all.each do |skill| %>
<%= check_box_tag "work[skill_ids][]", skill.id, #work.skill_ids.include?(skill.id), id: dom_id(skill) %>
<%= label_tag dom_id(skill), skill.title %><br>
<% end %>
The reason i'm doing this it because work can have many skills used.
I was going about this the wrong way. A join table solved the problem.
rails g migration create_skills_works_table
Then
class CreateSkillsWorksTable < ActiveRecord::Migration
def self.up
create_table :skills_works, :id => false do |t|
t.references :skill
t.references :work
end
add_index :skills_works, [:skill_id, :work_id]
add_index :skills_works, [:work_id, :skill_id]
end
def self.down
drop_table :skills_works
end
end
Using simple form on the work view.
<fieldset>
<legend>Skills Used </legend>
<%= f.association :skills %>
</fieldset>