rails paperclip: uploaded file does not get stored - ruby-on-rails

I am making my first steps with the Rails plugin 'paperclip' an therefor watched the RailsCast #134: http://railscasts.com/episodes/134-paperclip
Did everthing the same, except that I'm running rails 3.0.9 and installed paperclip (2.3.15) via adding it to the Gemfile.
Until 3:00 of the cast, everything works fine. But after reloading the show-page, I get the "missing" image instead of the uploaded image. Also, inside of the 'public' directory nothing new has been created.
Any hints?
Update: As requested here the relevant code:
Gemfile:
…
gem 'paperclip'
gem 'rails', '3.0.9'
…
config/routes.rb:
Foobar::Application.routes.draw do
resources :books
end
app/models/book.rb:
class Book < ActiveRecord::Base
has_attached_file :cover
attr_accessor :cover_file_name
end
app/controllers/books_controller.rb:
# nothing changed here after scaffolding
app/views/books/_form.html.erb:
<%= form_for(#book, :html => { :multipart => true}) do |f| %>
…
<div class="field">
<%= f.file_field :cover %>
</div>
app/views/books/show.html.erb:
…
<%= image_tag «book.cover.url %>
…
db/migrate/..._create_books.rb:
class CreateBooks < ActiveRecord::Migration
def self.up
create_table :books do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :books
end
end
db/migrate/..._ad_attachment_cover_to_book.rb:
class AddAttachmentCoverToBook < ActiveRecord::Migration
def self.up
add_column :books, :cover_file_name, :string
add_column :books, :cover_content_type, :string
add_column :books, :cover_file_size, :integer
add_column :books, :cover_updated_at, :datetime
end
def self.down
remove_column :books, :cover_file_name
remove_column :books, :cover_content_type
remove_column :books, :cover_file_size
remove_column :books, :cover_updated_at
end
end
I started up with "rails generate paperclip book cover" after having scaffold "book"

I think that the attr_accessor :cover_file_name creates a conflict for the table column with the same name. Try removing that line. Can't find anything else that should cause any problems.

People I think I already gave the solution when you appear this error is because the cover column does not have permission to enter this is solved in the controller of articles in the params_article can only manipulate the title and the body to this also adds to it To the column cover to me I stay like this
Def articulo_params params.require (: article) .permit (: title,: body,: cover)
End

Related

Hermitage Gem is not displaying my photos (ROR)

I have installed paperclip gem on the back end and would like to apply hermitage gem as my front-end; however, I am producing an error of undefined method file. Unlike the tutorial I did not use :file as my attribute, I named it :photo instead. Does that make a difference or the the problem occur from something else?
Error Message
undefined method `file' for #
view:
<%= render_gallery_for #galleries %>
migration
class AddAttachmentToGalleries < ActiveRecord::Migration
def self.up
add_attachment :galleries, :photo
end
def self.down
remove_attachment :galleries, :photo
end
end
According to readme https://github.com/ImmaculatePine/hermitage you should do something like this in your config/initializers/hermitage.rb
Hermitage.configure :posts do
original 'photo'
thumbnail 'photo(:thumbnail)'
end

Undefined method 'add_reference'

I'm trying to add a user reference to my post tables with following code:
class AddUserIdToPosts < ActiveRecord::Migration
def change
add_reference :posts, :user, index: true
end
end
but I've received an error message:
undefined method 'add_reference'
Anyone knows how to solve this?
I'm using Rails 3.2.13
In Rails 3 you must do it like so
class AddUserIdToPosts < ActiveRecord::Migration
def change
add_column :posts, :user_id, :integer
add_index :posts, :user_id
end
end
Only in Rails 4 you can do it the way you posted.
add_reference is specific to rails 4.0.0, so you should try this instead :
class AddUserIdToPosts < ActiveRecord::Migration
def change
add_column :posts, :user_id, :integer
add_index :posts, :user_id
end
end
this is a great post about this subject
Your migration should be
rails generate migration AddUserRefToPosts user:references
How about this:
def change
change_table :posts do |p|
p.references :user, index: true
end
end
This method apperead in Rails 4.0
I think you may create some monkey patch with this functionality for Rails 3.2

Creating a Rails 3 HABTM with Active Admin throws a 'Can't mass-assign protected attributes:' error

I am a rails noob so the below is probably down to lack of understanding however I have been looking/reading all day and cannot seem to find the solution.
I have two models - project and technology :
Project :
class Project < ActiveRecord::Base
attr_accessible description, :name
has_and_belongs_to_many :technologies, :join_table => :projects_technologies
end
Technology :
class Technology < ActiveRecord::Base
attr_accessible :abbr, :description, :name
has_and_belongs_to_many :projects, :join_table => :projects_technologies
end
My Create_Projects_Technologies migration was as follows :
class CreateProjectsTechnologies < ActiveRecord::Migration
def self.up
create_table :projects_technologies, :id => false do |t|
t.references :project
t.references :technology
end
add_index :projects_technologies, [:project_id, :technology_id]
add_index :projects_technologies, [:technology_id, :project_id]
end
def self.down
drop_table :projects_technologies
end
end
I am then using Active Admin to create and edit Project models using the following form :
ActiveAdmin.register Project do
form do |f|
f.inputs "Project attributes" do
f.input :name
f.input :description
f.input :technologies, as: :check_boxes
end
f.buttons
end
end
This correctly shows all my technologies as check boxes however as soon as I submit the form I hit the following error which I have not been able to overcome :
ActiveModel::MassAssignmentSecurity::Error in Admin::ProjectsController#update
Can't mass-assign protected attributes: technology_ids
All help is very much appreciate :D
Simple add technology_ids to Project attr_accessible
attr_accessible :client, :description, :name, :technology_ids

rails generate migration command to insert data into the table

I have a table and I had to add a migration script to add rows in the table.
Please help with the rails generate migration command to insert data into the table.
Thanks,
Ramya.
You can write regular ruby code inside a migration. So you can simply do something like this:
class Foo < ActiveRecord::Migration
def self.up
User.create(:username => "Hello", :role => "Admin")
end
def self.down
User.delete_all(:username => "Hello")
end
end
Just write regular ruby inside your migration same as you would in pry or rails console.
The code helped me is the sql statement as show
In migration file
def up
execute("insert into salary_ranges(salary_range) values('Above 2000');")
end
class AddFieldInUsers < ActiveRecord::Migration
def self.up
add_column :users, :admin, :boolean, :null => false, :default => 0
end
def self.down
remove_column :users
end
end

Rails Beginner Attempting TDD:

I'm new to unit testing and Rails in general. I've decided to build my projects in a TDD environment, but this has left me with some early questions.
I need help building the models to pass this test:
describe User do
it "should add user to team" do
team = Team.create(:name => "Tigers")
akash = User.create(:name => "Akash")
akash.teams << team
akash.memberships.size.should == 1
end
it "should allow buddyup"
john = User.create(:name => "John")
john.buddyup_with(akash)
john.memberships.size.should == 1
end
it "should validate linked buddys"
akash.buddys.should include(john)
end
end
Basically, ALL I want to do right now is pass the tests. Here is what I have so far:
class Team < ActiveRecord::Base
has_and_belongs_to_many :users
attr_accessubke :name
validates :name, :presence = true
:uniqueness => true
end
class User < ActiveRecord::Base
has_and_belongs_to_many: :teams
attr_accessible :name
validates :name, :presence = true
:uniqueness => true
end
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :users
end
end
class CreateTeams < ActiveRecord::Migration
def self.up
create_table :teams do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :teams
end
end
class CreateTeamsUsersJoinTable < ActiveRecord::Migration
def self.up
create_table :teams_users, :id => false do |t|
t.integer :team_id
t.integer :user_id
end
end
def self.down
drop_table :teams_users
end
end
That is all I have so far, and clearly it is nowhere near completion. Could you provide some insight, and perhaps code I should use to complete this? My biggest problem right now is the buddyup_with part. Adding a buddy will add a person to every team you are a member of, think of teams as parts of a development company, and buddys as understudies or something.
Suggestions I would make:
Use before do
# code #
end
to set up your conditions.
Do 1 test per. You have a lot going on there :)
Use Factory Girl.
Try what you have and work from there (Agile approach, even to adding tests).

Resources