Calling controller methods from a ruby script - ruby-on-rails

I'm trying to automatically pull ticker data from the Bitfinex bitcoin exchange and put it into a database for a Ruby on Rails project. Right now, I've got a controller BitfinexesController that does pull the data and create a new row on the corresponding database table bitfinexes when the method self.create is called. Right now I'm just trying to write a script that will call that method on a loop. I've tried to do this in the view but keep getting told that that there is an "undefined method 'create' for #".
Am I thinking about this wrong? I've just started learning Ruby on Rails.
Here's the method:
def self.create
newdata ="run script that grabs data"
#bitfinex = Bitfinex.new(eval(newdata))
respond_to do |format|
if #bitfinex.save
format.html { redirect_to #bitfinex, notice: 'Bitfinex was successfully created.' }
format.json { render :show, status: :created, location: #bitfinex }
else
format.html { render :new }
format.json { render json: #bitfinex.errors, status: :unprocessable_entity }
end
end
end`

Related

Rails store to different tables within the same controller

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

Controller to add current_user email to database in rails

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

New action created in a controller is not working

I am creating a Rails 4.2.6 /MongoDb app. I created a new action manually called 'calluser', made the change in routes to including it:
resources :companies do
member do
get 'calluser'
end
end
I can see it when I execute the 'rake routes' command:
call_user_company GET /companies/:id/call_user(.:format) companies#call_user
However, When I redirect from the controller:
if #company.save
format.html { redirect_to calluser_company(#company), notice: 'Company was successfully created.' }
format.json { render action: 'show', status: :created, location: #company }
else
format.html { render action: 'new' }
format.json { render json: #company.errors, status: :unprocessable_entity }
end
I receive the following error:
undefined method `calluser_company' for #<CompaniesController:0x007fdd893f3270>
Any Idea why this is happening ? I will appreciate any help.
I read a previous similar problems but they didn't work for me:
Create a new action for existing controller
Route a form to new controller action in Ruby on Rails
Your routes describes:
call_user_company GET /companies/:id/call_user(.:format) companies#call_user
so you can use call_user_company_url(#company) or call_user_company_path(#company) instead of calluser_company(#company)

Heroku Application Error After Form Submittal, But No Error Logged?

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.

Create more than one object at once using the standard create method in Ruby on Rails

I am trying to use the standard create method created for Ruby/Rails projects and simply pass in an additional form field that tells the method how many objects to create (vs just creating one object). The standard create method looks like so:
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
I want to pass in an additional data (form field called number_to_create) which tells the method how many of the microposts to create. I just added a new form field like this, in addition to the other micropost form field params:
<%= text_field_tag :number_to_create %>
My question is how do I modify the create method code such that it creates N number of micropost objects vs. just one. So if I pass in 3 from the form along with the other micropost attributes, the method creates 3 identical micropost objects, not just one as it currently does.
Thanks in advance for your help on this.
You could use the param as times
#microposts = Micropost.transaction do
[].tap do |microposts|
param[:number_to_create].times do
microposts << Micropost.create(micropost_params)
end
end
end
respond_to do |format|
if #microposts.all? &:persisted?
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
The transaction block is to make sure that either all of them gets saved, or none of them gets saved, this way you can fix your errors and recreate them without worrying of getting any stray saved objects

Resources