Web scraping information from different websites - ruby-on-rails

Im using a gem called MetaInspector to scrape data from different websites. Im building a site where i can collect data from different sites but am having trouble setting up. I have a model called site with a title and a url both strings. When i create a new "site" the name will come out as example.com/"sitename" and in there i would like to have the data just from that site. I kinda have an idea to this by adding page = MetaInspector.new to the new method but cant see how i can set a url in there.
I can show my controller and other info if needed.
Controller
class Admin::SitesController < Admin::ApplicationController
def index
#sites = Site.all
end
def show
#site = Site.friendly.find(params[:id])
end
def edit
#site = Site.friendly.find(params[:id])
end
def update
#site = Site.friendly.find(params[:id])
if #site.update(site_params)
redirect_to admin_path
else
render :edit
end
end
def destroy
#site = Site.friendly.find(params[:id])
#site.destroy
if #site.destroy
redirect_to admin_path
end
end
def new
#site = Site.new
end
def create
#site = Site.new(site_params)
if #site.save
redirect_to admin_path
else
render :new
end
end
private
def site_params
params.require(:site).permit(:title, :url)
end
end

If I understand correct you want to show the metainfo for a Site you have added. You could put that code in the show action of the controller:
def show
#site = Site.friendly.find(params[:id])
#page = MetaInspector.new(#site.url)
end
And update the show.html.erb template to display info about #page, ie:
<%= #page.title %>

Related

How to save nested route records in database?

Hello beautiful community. I am a newbie in Rails and I have a project where I have to register patients and each patient has evolutions. I have been able to register and save the patients in the database, but I cannot save the records of the evolution of these patients.
I am going to show my code for whoever can and wants to guide me. I would appreciate very much
#BACK OFFICE ROUTES-PACIENTES
resources :patients do
resources :evolutions
end
CONTROLLERS:
class PatientsController < ApplicationController
def create
#patient = current_user.patients.new patient_params
if #patient.save
return redirect_to patients_url, notice: 'El paciente fue agregado con exito'
end
render :new
end
class EvolutionsController < ApplicationController
before_action :authenticate_user!
def index
#evolutions = #patient.Evolution.all
end
def new
#evolution = Evolution.new
end
def edit
end
def create
#evolution = current_user.evolutions.new evolution_params
if #evolution.save
return redirect_to patients_url, notice: 'La evolucion fue agregada con exito'
end
render :new
end
def show
#evolution = Evolution.find params[:id]
end
private
def evolution_params
params.require(:evolution).permit(:motivo, :evolution, :patient_id)
end
end
As I was saying, the patients have no problems, but the evolutions I cannot save in the database.
I expect your ApplicationController already has a before_action :authenticate_user!, otherwise current_user wouldn't be working in your PatientsController. So, you can remove that.
In your Evolutions#index action you refer to the instance variable #patient despite this not having been set anywhere. Based on your routes this is a nested resource under Patient, and so your index action should look something like this:
def index
#patient = Patient.find(params[:patient_id]
#evolutions = #patient.evolutions # You could skip this and just use #patient.evolutions in your view if you like
end
Likewise, your new action should be modified to generate a new evolution for a specific patient:
def new
#patient = Patient.find(params[:patient_id]
#evolution = #patient.evolutions.build
end
This will create a new, unsaved instance of Evolution which already belongs to the appropriate user.
Finally, we need to update the create action to handle the nested resource also:
def create
#patient = Patient.find(params[:patient_id]
#evolution = #patient.evolutions.build(evolution_params)
if #evolution.save
redirect_to patients_url, notice: 'La evolucion fue agregada con exito'
else
render :new
end
end
As you can see, we now have repeated code in our actions for finding the Patient from the params ... we can DRY this up by moving it to a before action.
Pulling this all together:
class EvolutionsController < ApplicationController
before_action :load_patient
def index
#evolutions = #patient.evolutions # You could skip this and just use #patient.evolutions in your view if you like
end
def show
#evolution = #patient.evolutions.find(params[:id])
end
def new
#evolution = #patient.evolutions.build
end
def create
#evolution = #patient.evolutions.build(evolution_params)
if #evolution.save
redirect_to patients_url, notice: 'La evolucion fue agregada con exito'
else
render :new
end
end
def edit
end
private
def load_patient
#patient = Patient.find(params[:patient_id]
end
def evolution_params
params.require(:evolution).permit(:motivo, :evolution, :patient_id)
end
end

Reroute to users show page

When I create an event on the users show page I want it to redirect back to the users show page if it indeed submitted correctly. Is there anyway I can do that here is my Events controller:
class EventsController < ApplicationController
def show
#event = Event.find(params[:id])
#songs = #event.songs.paginate(page: params[:page])
end
def create
#event = current_user.events.build(event_params)
if #event.save
flash[:success] = "Event Created!"
redirect_to 'user#show'
else
render 'welcome#index'
end
end
def destroy
end
private
def event_params
params.require(:event).permit(:name, :partycode)
end
end
Redirect to the user path and pass in the User ID.
redirect_to user_path(current_user.id)
Yes you can, but you'll need to use the actual path eg instead of redirect_to 'user#show' user:
redirect_to user_path(#event.user)

Couldn't find Post without an ID POST

I am receiving this error:
ActiveRecord::RecordNotFound in MainController#compare Couldn't find
Post without an ID
Here is my code for main controller:
#job_one = Post.find(params[:j1])
#job_two = Post.find(params[:j2])
UPDATE #1:
main_controller.rb
class MainController < ApplicationController
skip_before_action :authenticate_user!
def index
#posts = Post.all.order(id: :asc)
end
def dummy
end
def editor
end
def compare
#job_one = Post.find(params[:j1])
#job_two = Post.find(params[:j2])
respond_to do |format|
format.html
format.pdf do
pdf = Prawn::Document.new
pdf.text "Hello World"
send_data pdf.render
end
end
end
end
What I am doing is comparing two posts that are made side by side. So these are the two posts I chose from the index page.
Can anyone off some assistance?

Clarification about using form_for

While creating a search form I am facing a problem. I am getting the following error:
undefined method `model_name' for NilClass:Class
This is my view file:
"datepicker" %>
This is my clients_controller.rb:
class ClientsController < ApplicationController
def newClients
end
end
And this is my model client.rb:
class Client < ActiveRecord::Base
# attr_accessible :title, :body
end
I am confused in using form_for parameter. Can any one explain it briefly how and why to use form_for parameter?
Edit 1
I have modified my controller as
class ClientsController < ApplicationController
def search
redirect_to root_path
end
end
Once i click submit button it showing error as
No route matches [GET] "/search"
You are missing something here. Let me explain.
In your controller you don't need to define a custom method (called newClients) since Rails conventions suggest to use the following:
class ClientsController < ApplicationController
# GET /clients
def index
#clients = Client.all
end
# GET /clients/:id
def show
#client = Client.find(params[:id])
end
# GET /clients/new
def new
#client = Client.new
end
# POST /clients
def create
#client = Client.new(params[:client])
if #client.save
redirect_to :back, success: "Successfully created..."
else
render :new
end
end
# GET /clients/:id/edit
def edit
#client = Client.find(params[:id])
end
# PUT /clients/:id
def update
#client = Client.find(params[:id])
if #client.update_attributes(params[:client])
redirect_to :back, success: "Successfully edited..."
else
render :edit
end
end
# DELETE /clients/:id
def destroy
#client = Client.find(params[:id]).destroy
redirect_to :back, success: "Successfully deleted..."
end
end
And finally, in order for your form_for to work properly, you need to pass it an instance of a class:
form_for #client
where #client is Client.new in your case.
First of all in your controller please follow Rails naming conventions. The method name should be new_clients or new.
def new
#client = Client.new
end
Your view name should be new.html.erb.
You are not defining #client in your controller, but in the view you are using it.

Render page and pass parameters

I have three html pages.
In the first page, I view the list of data.
In the second page, I view the data of a particular element of the list.
In the third page, the user can edit data of a particular element of the list.
When the user submits the 'form', how can I redirect the user in the second page? I tried in this way:
render :action => "show_details",:id=>params[:id]
It works. The link is correct. But the page is not opened if I do not refresh the page.
UPDATE I
I write my code in this action in the reports controller:
def setFixed
rs=Report.find(params[:id])
rs.state ="1"
rs.save
render :action => "show_details",:id=>params[:id]
end
UPDATE II
Reports controller code:
class ReportsController < ApplicationController
before_filter :authenticate_user, :only => [:index,:show,:show_details,:new]
def stateDialog
render :stateDialog, :current_state=>params[:current_state]
end
def setFixed
rs=Report.find(params[:id])
rs.state ="1"
rs.save
render :action=>"show_details",:id=>params[:id]
end
def setNotFixed
rs=Report.find(params[:id])
rs.state ="0"
rs.save
render :action=>"show_details",:id=>params[:id]
end
def edit
#report=Report.find(params[:id])
end
def update
#report = Report.find(params[:id])
if #report.update_attributes(params[:report])
flash[:notice]=''
render :action=>"show_details",:id=>params[:id]
else
flash[:notice]=''
render :action=>"show_details",:id=>params[:id]
end
end
def deleteDialog
render "deleteDialog"
end
def focus_maps
render "focus_maps"
end
def delete
Report.find(params[:id]).destroy
render "show"
end
def index
#report=Report.new
end
def logged
render "new"
end
def show
render params[:page]
end
def new
#report=Report.new
end
def show_details
render "show_details"
end
def create
#report=Report.new( params[:report] )
if #report.save
flash[:notice]='Segnalazione avvenuta!'
else
flash[:notice]='Impossibile sottoporre la segnalazione!'
end
render "show"
end
end
I found advice that params must be filled before render calling, like this:
#id = params[:id]
render :action => 'show_details'
This solution is works for me, try it
If you want to redirect user somewhere you should use redirect_to
redirect_to action: 'show_details', id: params[:id]

Resources