Last active
May 29, 2017 16:46
-
-
Save rvgarimrj/d668355ea1de45288f6f985eebafe08b to your computer and use it in GitHub Desktop.
Metodo create com params não está chamando corretamente ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CompaniesController < ApplicationController | |
before_action :authenticate_api_v1_user!, :set_company, only: [:show, :update, :destroy] | |
# GET /companies | |
# GET /companies.json | |
def index | |
@companies = Company.all | |
end | |
# GET /companies/1 | |
# GET /companies/1.json | |
def show | |
end | |
# POST /companies | |
# POST /companies.json | |
def create | |
@company = Company.new(company_params) | |
binding.pry | |
if @company.save | |
render :show, status: :created | |
else | |
binding.pry | |
render json: { | |
status: 500, | |
errors: @company.erros | |
}.to_json | |
end | |
end | |
# PATCH/PUT /companies/1 | |
# PATCH/PUT /companies/1.json | |
def update | |
if @company.update(company_params) | |
render :show, status: :ok | |
else | |
render json: { | |
status: 500, | |
errors: @company.erros | |
}.to_json | |
end | |
end | |
# DELETE /companies/1 | |
# DELETE /companies/1.json | |
def destroy | |
@company.destroy | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_company | |
@company = Company.find(params[:id]) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def company_params | |
params.require(:company).permit(:company_name, :short_name, :kind, :cpf_cnpj, :street, :number, :city, :zipcode, :state, :neighborhood, :phone, :user_id,:company_email) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Company < ApplicationRecord | |
enum type: [ :cpf, :cnpj ] | |
validates :company_name, :short_name, :company_email, presence: true, uniqueness: true | |
# validates :company_name, presence: { message: "must be given please" } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
# Include default devise modules. | |
# :confirmable, | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable, | |
:omniauthable | |
include DeviseTokenAuth::Concerns::User | |
mount_base64_uploader :photo, PhotoUploader | |
belongs_to :company, dependent: :destroy | |
enum type: [ :dono, :vendedor ] | |
validates :kind_of_user, :presence => true | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api::V1::UsersController < ApplicationController | |
before_action :authenticate_api_v1_user! | |
# before_action :set_user, only: [:update] | |
def update | |
@user = current_api_v1_user | |
begin | |
if params[:company] | |
if @user.company.present? | |
binding.pry | |
@user.company.update(company_params) | |
else | |
binding.pry | |
@user.update(company: Company.create(company_params)) | |
end | |
end | |
@user.update(user_params) | |
# render template: '/api/v1/users/show', status: 200 | |
rescue Exception => errors | |
render json: errors, status: :unprocessable_entity | |
end | |
end | |
def current_user | |
@user = current_api_v1_user | |
render template: '/api/v1/users/show', status: 200 | |
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, :nickname, :photo, :email, :company_id, :kind_of_user) | |
end | |
def company_params | |
params.require(:company).permit(:company_name, :short_name, :kind, :cpf_cnpj, :street, :number, :city, :zipcode, :state, :neighborhood, :phone,:company_email) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment