I'm setting up a photo blog with Rails and am having difficulty with this error: Excon::Errors::SocketError in PostsController#create
Connection reset by peer (Errno::ECONNRESET)
Extracted source (around line #30):
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
(0.2ms) BEGIN
SQL (0.3ms) INSERT INTO "posts" ("title", "image", "description", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["title", "Title test"], ["image", "mcm-magnavox-stereo-1.jpg"], ["description", "Description test"], ["created_at", "2016-02-19 22:13:34.023935"], ["updated_at", "2016-02-19 22:13:34.023935"]]
(0.9ms) ROLLBACK
Completed 500 Internal Server Error in 6027ms (ActiveRecord: 1.6ms)
Excon::Errors::SocketError (Connection reset by peer (Errno::ECONNRESET)):
app/controllers/posts_controller.rb:30:in `block in create'
app/controllers/posts_controller.rb:29:in `create'
Here's my post model:
class Post < ActiveRecord::Base
validates_presence_of :title, :image
mount_uploader :image, PhotoUploader
end
Here's my post controller:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :image, :description)
end
end
Uploader
# encoding: utf-8
class PhotoUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support: # include CarrierWave::RMagick include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: # include Sprockets::Helpers::RailsHelper # include Sprockets::Helpers::IsolatedHelper
# Include the sprockets-rails helper for Rails 4+ asset pipeline compatibility: include Sprockets::Rails::Helper
# Choose what kind of storage to use for this uploader: # storage :file storage :fog
# Override the directory where uploaded files will be stored. # This is a sensible default for uploaders that are meant to be mounted: def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end
# Provide a default URL as a default if there hasn't been a file uploaded: # def default_url # # For Rails 3.1+ asset pipeline compatibility: # # asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) # # "/images/fallback/" + [version_name, "default.png"].compact.join('_') # end
# Process files as they are uploaded: #process :resize_to_fill => [200, 200] # # def scale(width, height) # # do something # end
# Create different versions of your uploaded files: version :tiny do
process :resize_to_fill => [20, 20] end
version :profile_size do
process :resize_to_fill => [300, 300] end
# version :full_size do # process :resize_to_fill => [700, 700]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this: def extension_white_list
%w(jpg jpeg gif png) end
# Override the filename of the uploaded files: # Avoid using model.id or version_name here, see uploader/store.rb for details. # def filename # "something.jpg" if original_filename # end
end
Carrierwave config (fog.rb)
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => "AWS",
:aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
config.fog_directory = ENV['AWS_BUCKET']
config.fog_public = false
config.fog_region = ENV['AWS_REGION']
end
I didn't have a value for my region. I set us-west-1 first and that returned an error but changing the region to us-east-1 worked.
Thank you Geemus!
Related
Can't add or edit a new micropost in my toy_app (Chapter 2 of the Rails Tutorial). Before I added users and microposts with no problem. Then I specified the has_many / belongs_to relationship and ever since I'm getting:
"NoMethodError in MicropostsController#create undefined method `content' for
Did you mean? context context= context?" when I try to create and microposts, and a similar message when updating a micropost.
It might be worth nothing that I only had one user, and the user id was "3"... not sure why it wouldn't be 1. I've since deleted all microposts and still only have 1 user. How do I avoid this error message? If anyone could help I would greatly appreciate it.
The Microposts Controller:
class MicropostsController < ApplicationController
before_action :set_micropost, only: [:show, :edit, :update, :destroy]
# GET /microposts
# GET /microposts.json
def index
#microposts = Micropost.all
end
# GET /microposts/1
# GET /microposts/1.json
def show
end
# GET /microposts/new
def new
#micropost = Micropost.new
end
# GET /microposts/1/edit
def edit
end
# POST /microposts
# POST /microposts.json
def create
#micropost = Micropost.new(micropost_params)
respond_to do |format|
if #micropost.save
format.html { redirect_to #micropost, notice: 'Micropost was successfully created.' }
format.json { render :show, status: :created, location: #micropost }
else
format.html { render :new }
format.json { render json: #micropost.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /microposts/1
# PATCH/PUT /microposts/1.json
def update
respond_to do |format|
if #micropost.update(micropost_params)
format.html { redirect_to #micropost, notice: 'Micropost was successfully updated.' }
format.json { render :show, status: :ok, location: #micropost }
else
format.html { render :edit }
format.json { render json: #micropost.errors, status: :unprocessable_entity }
end
end
end
# DELETE /microposts/1
# DELETE /microposts/1.json
def destroy
#micropost.destroy
respond_to do |format|
format.html { redirect_to microposts_url, notice: 'Micropost was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_micropost
#micropost = Micropost.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def micropost_params
params.require(:micropost).permit(:context, :user_id)
end
end
Microposts model:
class Micropost < ApplicationRecord
belongs_to :user
validates :content, length: { maximum: 140 }
end
Users model:
class User < ApplicationRecord
has_many :microposts
end
Users Controller
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
#users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
#user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if #user.update(user_params)
format.html { redirect_to #user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: #user }
else
format.html { render :edit }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
#user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :email)
end
end
schema.rb:
ActiveRecord::Schema.define(version: 20171222133429) do
create_table "microposts", force: :cascade do |t|
t.text "content"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
In your micropost_params you have permitted context. I assume it should be a content. Please check your schema.rb to verify what columns does your microposts table have.
I am trying to save in 2 tables in the same time, using nested form. I do not know why this is not happening. Rails error says "1 error prohibited this person from being saved:" Why the Legal Person is not getting the Person_id?
Models
class Person < ApplicationRecord
has_one :legal_person, dependent: :destroy
accepts_nested_attributes_for :legal_person
end
class LegalPerson < ApplicationRecord
belongs_to :person
end
Controller
class PeopleController < ApplicationController
before_action :set_person, only: [:show, :edit, :update, :destroy]
# GET /people
# GET /people.json
def index
#people = Person.all
end
# GET /people/1
# GET /people/1.json
def show
end
# GET /people/new
def new
#person = Person.new
#person.build_legal_person
end
# GET /people/1/edit
def edit
end
# POST /people
# POST /people.json
def create
#person = Person.new(person_params)
respond_to do |format|
if #person.save
format.html { redirect_to #person, notice: 'Person was successfully created.' }
format.json { render :show, status: :created, location: #person }
else
format.html { render :new }
format.json { render json: #person.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /people/1
# PATCH/PUT /people/1.json
def update
respond_to do |format|
if #person.update(person_params)
format.html { redirect_to #person, notice: 'Person was successfully updated.' }
format.json { render :show, status: :ok, location: #person }
else
format.html { render :edit }
format.json { render json: #person.errors, status: :unprocessable_entity }
end
end
end
# DELETE /people/1
# DELETE /people/1.json
def destroy
#person.destroy
respond_to do |format|
format.html { redirect_to people_url, notice: 'Person was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_person
#person = Person.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def person_params
#params.require(:person).permit(:nome, :tipopessoa, :telefonefixo, :telefonecelular)
params.require(:person).permit(:nome, :tipopessoa, :telefonefixo, :telefonecelular,
legal_person_attributes: LegalPerson.attribute_names.map(&:to_sym))
#legal_person_attributes: [:id, :cnpj, :nomefantasia, :person_id])
end
end
Error
Parameters: {"utf8"=>"✓", "authenticity_token"=>"pAAOYapSgnF8H6O2cJMpHSN6POBwAzycy00Tij5TKFDv+8d+EErtdQGKuRbay4IxKrUQjXVPq5MblVgjv5Fa5g==", "person"=>{"nome"=>"qweqw", "tipopessoa"=>"wqeqweq", "telefonefixo"=>"qweqe", "telefonecelular"=>"qweqw", "legal_person_attributes"=>{"cnpj"=>"qweqw", "nomefantasia"=>"wqewe"}}, "commit"=>"Criar Person"}
(0.1ms) BEGIN
(0.2ms) ROLLBACK
use attribute name [:cnpj, :nomefantasia] instead of
LegalPerson.attribute_names.map(&:to_sym)
Firstly apologies as just when i think i have a grip on routing i find that i don't!!
When a registered user logs in to my app they are taken to the admin index view and all i am trying to do is display their user name within that view.
This is my routes config: -
Easygifts::Application.routes.draw do
get 'admin' => 'admin#index'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
get "sessions/create"
get "sessions/destroy"
resources :users
resources :admin
resources :stores do
collection do
get "writing"
get "office"
get "time"
get "home"
get "wellness"
get "travel"
get "bags"
get "leisure"
get "contact"
get "terms"
end
member do
get 'quote'
end
end
resources :products
Users login via the sessions controller and are passed on to the Admin controllers index view. Where i am trying to display their name using the following: - <th colspan="2">Welcome <%= #users.name %></th>
The Sessions controller: -
class SessionsController < ApplicationController
skip_before_action :authorize
def new
end
def create
user = User.find_by(name: params[:name])
if user and user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to admin_url
else
redirect_to login_url, alert: "Invalid user/password combination"
end
end
def destroy
session[:user_id] = nil
redirect_to stores_url, notice: "Logged out"
end
end
The Users Controller: -
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
#users = User.order(:name)
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
#user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
format.html { redirect_to users_url, notice: "User #{#user.name} was successfully created." }
format.json { render action: 'show', status: :created, location: #user }
else
format.html { render action: 'new' }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if #user.update(user_params)
format.html { redirect_to users_url, notice: "User #{#user.name} was successfully updated." }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
begin
#user.destroy
flash[:notice] = "User #{#user.name} deleted"
rescue StandardError => e
flash[:notice] = e.message
end
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :password, :password_confirmation, :add1, :add2, :add3, :add4, :post_code, :home_tel, :mobile, :ice_tel, :ni_number, :position, :image_url, :staff_num, :start_date, :birthday)
end
end
And the Admin Controller: -
class AdminController < ApplicationController
def index
#users = User.find(params[:id])
end
end
My first point of confusion is whether i should be going through the Sessions controller or the Users controller?
If it's in the Sessions controller am i supposed to write something like get 'admin#index' => :show
Or if it's in Users am i to write something like resources :users do member do get 'admin' end end
I think i have to define a controller with an action within my routes but i don't understand in this instance how to work out who is doing what.
Here, I want a clear concept about uploading file or image without using any gemfile(Ex: peperclip, carrierwave etc).
Previously, I did some work. Where i can upload images in "/assets/images" folder. But i could't see images when i call it in my show page(there was only a placeholder of break image).
here is the screenshot of show page:
how can i show those images in my show.html.erb file?
Here is my ImagesController.rb file:
class ImagesController < ApplicationController
before_action :set_image, only: [:show, :edit, :update, :destroy]
# GET /images
def index
#images = Image.all
end
# GET /images/1
def show
end
# GET /images/new
def new
#image = Image.new
end
# GET /images/1/edit
def edit
end
# POST /images
def create
#image = Image.new(image_params)
if params[:image].present?
file = params[:image][:picture]
File.open(Rails.root.join('app','assets', 'images', file.original_filename), 'wb') do |f|
f.write(file.read)
end
end
respond_to do |format|
if #image.save
format.html { redirect_to #image, notice: 'Image was successfully created.' }
format.json { render action: 'show', status: :created, location: #image }
else
format.html { render action: 'new' }
format.json { render json: #image.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #image.update(image_params)
format.html { redirect_to #image, notice: 'Image was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #image.errors, status: :unprocessable_entity }
end
end
end
# DELETE /images/1
def destroy
#image.destroy
respond_to do |format|
format.html { redirect_to images_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_image
#image = Image.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def image_params
params.require(:image).permit(:name)
end
end
Here is my show.html.erb file:
<p><%= #image.name %></p>
<p><%= image_tag #image.picture %></p>
Here is my schema.rb file:
ActiveRecord::Schema.define(version: 20140725043842) do
create_table "images", force: true do |t|
t.string "name"
t.string "picture"
t.datetime "created_at"
t.datetime "updated_at"
end
end
please tell me, if you need more information about my project.
please give me some advice. I want a clear understand about uploading image/file and show them without using gemfile.
You have to upload images to a folder that doesn't need precompiling to be show, as assets do. Try uploading them to public folder.
Also you might want to read about the asset pipeline and why aren't they displaying:
http://guides.rubyonrails.org/asset_pipeline.html
I am trying to set the associations . By now I have both models Post and Comment with their associations (in theory).
The problem is that when I try to see the comment of a specific post it says that such method does not exist.
I am sure forgetting something kinda obvious but I can't figure it out.
Thanks in advance!
MODELS
class Post < ActiveRecord::Base
has_many :comments, dependent: :destroy
end
class Comment < ActiveRecord::Base
belongs_to :post
end
And i got the error on rails console:
p = Post.all
p[0].comments
"undefined method comments for post"
Controllers:
Coment:
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
# GET /comments
# GET /comments.json
def index
#comments = Comment.all
end
# GET /comments/1
# GET /comments/1.json
def show
end
# GET /comments/new
def new
#comment = Comment.new
end
# GET /comments/1/edit
def edit
end
# POST /comments
# POST /comments.json
def create
#comment = Comment.new(comment_params)
respond_to do |format|
if #comment.save
format.html { redirect_to #comment, notice: 'Comment was successfully created.' }
format.json { render action: 'show', status: :created, location: #comment }
else
format.html { render action: 'new' }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
respond_to do |format|
if #comment.update(comment_params)
format.html { redirect_to #comment, notice: 'Comment was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
#comment.destroy
respond_to do |format|
format.html { redirect_to comments_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
#comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:post_id, :body)
end
end
Post:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: #post }
else
format.html { render action: 'new' }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :body)
end
end
Try this
has_many :comments, :class_name => "Comment", :foreign_key => :post_id
I think this is because you're trying to pull all comments for all posts, try this in rails console. This will only pull the first Post and then its related comment.
p = Post.first
p.comments
Are you getting an error on the view as well? If so, can you post the code from the view in question.