I have an application, which I only have configured for one company, I need to change it, so that it accepts multiple companies, as follows:
When you create a company, create the independent db for that company and in turn the user, later when that user with that company_id authenticates, connect to that previously created database.
def create
#datos_empresa = DatosEmpresa.new(datos_empresa_params)
respond_to do |format|
if #datos_empresa.save
client = Mysql2::Client.new(:host => 'localhost', :username=>"*****", :password=> "*********")
nombre_db = 'name_company'+#datos_empresa.nombre
client.query("CREATE DATABASE #{nombre_db.downcase}")
ActiveRecord::Base.establish_connection(client)
format.html { redirect_to #datos_empresa, notice: 'Datos empresa was successfully created.' }
format.json { render :show, status: :created, location: #datos_empresa }
else
format.html { render :new }
format.json { render json: #datos_empresa.errors, status: :unprocessable_entity }
end
end
end
The best way I found of doing this on my application was using the ros-apartment gem, it handles all the DB creation, migrations (bet you forgot that one), seeds and switching.
Related
I have two models, Livestock and History
a livestock has many histories and history belongs to livestock
This is the create method inside the LivestockController
# POST /livestocks
# POST /livestocks.json
def create
#livestock = Livestock.new(livestock_params.permit!)
respond_to do |format|
if #livestock.save
format.html { redirect_to #livestock }
flash[:success] = "Livestock was successfully created"
format.json { render :show, status: :created, location: #livestock }
else
format.html { render :new }
format.json { render json: #livestock.errors, status: :unprocessable_entity }
end
end
end
I wanted to create a record in the histories table with
history = History.new(livestock_id: #livestock.id, event: "Purchased", event_date: #livestock.purchase_date, image: #livestock.image)
history.save!
inside the create method
How can I do it? I can't put it in the create method because it says
Validation failed: Livestock must exist
apparently #livestock has not yet have the id attribute
Edit:
it still raises the same exception when I put it after
if #livestock.save
However I found a work around by using the session variable. Since it is redirected to the show page, I created the following inside the create method
session[:created] = "created"
And in my show method
# GET /livestocks/1
# GET /livestocks/1.json
def show
if session[:created] == "created"
history = History.new(livestock_id: params[:id], event: "Purchased", event_date: #livestock.purchase_date, image: #livestock.image)
history.save!
session.delete(:created)
end
end
Now I am wondering what are consequences if I use this approach.
Livestock record is created when you call save (and there is no validation error). So one option is to create the history inside this if condition:
if #livestock.save
Another option is to use after_create callback in the livestock model that will create a history object right after creating livestock. You have to be careful because the callback might be called when you do not need it (i.e. when importing data).
The last option is to create a separate service object that will create livestock and all other required objects. That's probably the best approach, but it will require more customized code.
Update
Please also make sure to move if/else block outside the respond_to block:
if #livestock.save
# create history object here
respond_to do |format|
format.html { redirect_to #livestock }
flash[:success] = "Livestock was successfully created"
format.json { render :show, status: :created, location: #livestock}
end
else
respond_to do |format|
format.html { render :new }
format.json { render json: #livestock.errors, status: :unprocessable_entity }
end
end
I have a column in my database that I'm trying to update in my controller. I'm trying to take the current logged in user's email and send it to the column in the model using the create method. It's not sending the email to the database though.
Here's my create method in the controller
def create
#request = Request.new(request_params)
respond_to do |format|
if #request.save
format.html { redirect_to #request, notice: 'Request was successfully created.' }
format.json { render :show, status: :created, location: #request }
#request.email = current_user.email
#request.increment!(:voteCount)
else
format.html { render :new }
format.json { render json: #request.errors, status: :unprocessable_entity }
end
end
end
I'm fairly certain the trouble lies in the line #request.email = current_user.email but Im not sure why.
The user model and the request models are different, so im trying to grab the data from one model and send it to another.
Sergio's comment helped me figure this out.
Added #request.save
I get an error when I try to create a new form with image uploading to Amazon S3.
I think its because of my photo uploads, but not too sure. when I do this in my development environment, no issues. I'm using Amazon S3, in development as well.
I checked heroku logs and I get no error.
Once I create a new form, it supposed to direct me to that show.html.erb page, with the id in the URL (ie: www.example.com/projects/this-is-new-page), but instead, it sent me to www.example.com/projects and the error.
Oh, I'm also using friendly_id gem
def create
#project = project.new(project_params)
respond_to do |format|
if #project.save
if params[:photos]
params[:photos].each { |image|
#project.project_images.create(photo: image)
}
end
format.html { redirect_to #project, notice: 'Trip was successfully created.' }
format.json { render :show, status: :created, location: #project }
else
format.html { render :new }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
When I go back to www.example.com/projects/this-is-new-page, it works. But I only get this error right after I upload or submit a form.
We are developing a Rails app that integrates with an existing application. We are using Mule and restful interfaces to move data between the 2 applications.
We have a few code tables like Priority and Types that come from the old app to the new app. If we can use the same ID numbers for both applications, it will be easier to move data records that use those IDs for foreign-keys.
Is there a way to allow the ID to be one of the fields that gets updated via the Rails restful interface?
I would like this to work:
http://localhost:5000/types?type[id]=315&type[typecode]=test
This is the type controller for POST:
# POST /types
# POST /types.json
def create
#type = Type.new(params[:type])
respond_to do |format|
if #type.save
format.html { redirect_to #type, notice: 'Type was successfully created.' }
format.json { render json: #type, status: :created, location: #type }
format.xml { render xml: #type, status: :created, location: #type }
else
format.html { render action: "new" }
format.json { render json: #type.errors, status: :unprocessable_entity }
format.xml { render xml: #type.errors, status: :unprocessable_entity }
end
end
end
Right now, if I post that URL, it creates a new type record with typecode=test, but the id is the next available id instead of the one I gave it in the URL.
Thanks for the help!
Did you try this?
#type = Type.new(params[:type])
if params[:type][:id]
#type.id = params[:type][:id]
end
#type.save
I have a three part user signup that work with two separate models: User, and Customer Info. There's a user signup that I submit remotely, and it goes to the next part of the form. I want to be able to use the current_user variable when I submit the Customer Info row. The following code works when I test without remote, but it just doesn't even create the row at all when I do it with the remote. Is there another variable I can use in lieu of this variable? Or is there a better way of accomplishing this?
def create
#customer_info = CustomerInfo.new(params[:customer_info].merge(:user_id => current_user.id))
respond_to do |format|
if #customer_info.save
format.html { redirect_to tasks_url, notice: 'Customer info was successfully created.' }
format.json { render json: tasks_url, status: :created, location: #customer_info }
else
format.html { render action: "new" }
format.json { render json: #customer_info.errors, status: :unprocessable_entity }
end
end
end
Turns out it was a devise config situation. I wasn't allowing users to be unconfirmed for any amount of time, so "curent_user" was staying empty even after they signed up. I added config.allow_unconfirmed_access_for = 2.days and restarted the server and that worked.