Weird route behaivor for show view - ruby-on-rails

I am making an application with all the model names in spanish. I am having some strange issues related with the singularization.
My model:
class Artista < ActiveRecord::Base
attr_accessible :fecha, :foto, :instrumento, :nombre
end
My model name is "artista" (artist) in singular.
Controller:
class ArtistasController < ApplicationController
# GET /bandas
# GET /bandas.json
def index
#artistas = Artista.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #artistas }
end
end
def show
#artista = Artista.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #artista }
end
end
def new
#artista = Artista.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #artista }
end
end
def edit
#artista = Artista.find(params[:id])
end
def create
#artista = Artista.new(params[:artista])
respond_to do |format|
if #artista.save
format.html { redirect_to #artista, notice: 'Artista was successfully created.' }
format.json { render json: #artista, status: :created, location: #artista }
else
format.html { render action: "new" }
format.json { render json: #artista.errors, status: :unprocessable_entity }
end
end
end
def update
#artista = Artista.find(params[:id])
respond_to do |format|
if #artista.update_attributes(params[:banda])
format.html { redirect_to #artista, notice: 'Artista was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #artista.errors, status: :unprocessable_entity }
end
end
end
def destroy
#artista = Artista.find(params[:id])
#artista.destroy
respond_to do |format|
format.html { redirect_to artistas_url }
format.json { head :no_content }
end
end
end
(All this has been automatically created with the rails generate commands)
Now, my routes include the following:
resources :artistas
When I access localhost:3000/artistas everything works great. I can see the list of already created aritsts. Now, when I click on an existing artist (or after I try to create a new one, being redirected to the show artist page) for some strange reason it goes to http://localhost:3000/artistum.3 (3 being the id of the artist I clicked on). The output for that url is a completely blank page.
I have never even typed the word artistum. I don't know where it got it from. Besides, it has a dot instead of a slash to separate the name from the id, so i dont know how to redirect it.
I ran a grep search of the folder containing everything and the word artistum exists only in log files.
My guess is that somehow part of my application thinks "artista" is plural and "artistum" is its singular form.
I added to my routes match '/artistum' => 'artistas#index'and that works for the index page, but the dot has me confused on how to do it for the show pages.
Can someone help me A) find out why its trying to get there or b) how to route from those show pages?
Thanks!

You could try this:
Add this to the inflections.rb in the config/initializers folder:
ActiveSupport::Inflector.inflections do |inflect|
inflect.plural 'artista', 'artistas'
inflect.irregular 'artista', 'artistas'
end

Related

Rails: Method to check the database appointment is not taken - not saving

I have a very beginners understanding of rails and ruby and I keep
getting stuck. If anyone could please point out my where I'm going wrong, that would be great! Or else, is there an easier way to validate the database before I allow an appointment? I don't want double bookings.
I am making an appointment booking app and I have a very basic design. I
have created an appointments scaffold with name:string phone:string
email:string numpeople:integer date:date timeslot:string.
In the view for creating a new appointment I have stated that appointment 1 is
9-11am, appointment 2 is 12-2pm, appointment 3 is 3-5pm and appointment
4 is 5 - 7pm. The user is asked to enter 1,2,3 or 4.
When the user clicks on "make appointment" I'm trying to interrupt the
appointments controller (create method) so that I can check if the date
&& timeslot are nil. if that is the case, the system should continue on
to create the appointment, if not then I want to redirect to somewhere
else. I have created a method called isValid? in the model (See below)
I think the method is correct as the system is getting as far as the
redirect. Tclass Appointment < ActiveRecord::Base
class Appointment < ActiveRecord::Base
def isValid?
taken = Appointment.where("date = ? && timeslot = ?", date, timeslot)
save unless taken
end
end
The problem is, it keeps redirecting to the page I told it to
go to if it's not saved(the homepage or root_path). (Also the
appointments are not saving).
appointments controller create method:
def create
valid = #appointment = Appointment.new(appointment_params).isValid?
respond_to do |format|
if valid
format.html { redirect_to new_appointment_path, notice: 'Appointment was
successfully created.' }
format.json { render :show, status: :created, location: #appointment }
else
format.html { redirect_to appointments_path, notice: 'That appointment
is not available, please choose again' } # this redirect works with no
notice
format.js { render json: #appointment.errors, status:
:unprocessable_entity }
end
end
end
Full appointments controller class: (In case I've missed something)
class AppointmentsController < ApplicationController
before_action :set_appointment, only: [:show, :edit, :update, :destroy]
def index
#appointments = Appointment.all
end
def show
end
def new
#appointment = Appointment.new
end
def edit
end
def create
valid = #appointment = Appointment.new(appointment_params).isValid?
respond_to do |format|
if valid
format.html { redirect_to new_appointment_path, notice: 'Appointment was
successfully created.' }
format.json { render :show, status: :created, location: #appointment }
else
format.html { redirect_to appointments_path, notice: 'That appointment
is not available, please choose again' } # this redirect works with no
notice
format.js { render json: #appointment.errors, status:
:unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #appointment.update(appointment_params)
format.html { redirect_to #appointment, notice: 'Appointment was
successfully updated.' }
format.json { render :show, status: :ok, location: #appointment }
else
format.html { render :edit }
format.json { render json: #appointment.errors, status:
:unprocessable_entity }
end
end
end
def destroy
#appointment.destroy
respond_to do |format|
format.html { redirect_to appointments_url, notice: 'Appointment was
successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_appointment
#appointment = Appointment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list
through.
def appointment_params
params.require(:appointment).permit(:name, :phone, :email, :numpeople,
:date, :timeslot)
end
end
I think your method should be something like:
class Appointment < ActiveRecord::Base
def isValid?
taken = Appointment.where("date = ? && timeslot = ?", date, timeslot)
save unless taken.present?
end
end
Let me know if that works.

Rails 4.2 NameError in CandiesController#create

Trying to build a route that can display pages with varying information about various types of candy.
the route recognizes URL paths but want it to only show valid candy types e.g kit_kat, gummy_bear, twizzler Any other type of candy specified should generate a 404 status code
Generated a scaffold to allow anyone to add candy types but when i try to pass the valid candy types ( kit_kat etc) I get error
Rails 4.2 NameError in CandiesController#create
undefined local variable or method ` params' for #
**candy_controller.rb**
class CandiesController < ApplicationController
before_action :set_candy, only: [:show, :edit, :update, :destroy]
# GET /candies
# GET /candies.json
def index
#candies = Candy.all
end
# GET /candies/1
# GET /candies/1.json
def show
end
# GET /candies/new
def new
#candy = Candy.new
end
# GET /candies/1/edit
def edit
end
# POST /candies
# POST /candies.json
def create
if (([:kit_kat, :skittles, :m_and_ms, :herseys_kiss, :butterfinger, :gummy_bear,
:twizzler]).any? { |word| params[:title].includes?(word) })
#candy = Candy.new(candy_params)
respond_to do |format|
if #candy.save
format.html { redirect_to #candy, notice: 'Candy was successfully created.' }
format.json { render :show, status: :created, location: #candy }
else
format.html { render :new }
format.json { render json: #candy.errors, status: :unprocessable_entity }
end
end
end
end
# PATCH/PUT /candies/1
# PATCH/PUT /candies/1.json
def update
respond_to do |format|
if #candy.update(candy_params)
format.html { redirect_to #candy, notice: 'Candy was successfully updated.' }
format.json { render :show, status: :ok, location: #candy }
else
format.html { render :edit }
format.json { render json: #candy.errors, status: :unprocessable_entity }
end
end
end
# DELETE /candies/1
# DELETE /candies/1.json
def destroy
#candy.destroy
respond_to do |format|
format.html { redirect_to candies_url, notice: 'Candy was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_candy
#candy = Candy.friendly.find(params[:id])
end
# Use callbacks to share common setup or constraints between actions.
# Never trust parameters from the scary internet, only allow the white list through.
def candy_params
params.require(:candy).permit(:title, :discription)
end
end
candy.rb
class Candy < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
end
updated candy_controller.rb
def create
if candy[:title] && !candy[:title].empty? && [:kit_kat, :skittles, :m_and_ms, :herseys_kiss, :butterfinger, :gummy_bear,
:twizzler].include?(candy[:title].to_sym)
#candy = Candy.new(candy_params)
respond_to do |format|
if #candy.save
format.html { redirect_to #candy, notice: 'Candy was successfully created.' }
format.json { render :show, status: :created, location: #candy }
else
format.html { render :new }
format.json { render json: #candy.errors, status: :unprocessable_entity }
end
end
end
end
updated code
def create
if candy_params[:title] && !candy_params[:title].empty? && [:kit_kat, :skittles, :m_and_ms, :herseys_kiss, :butterfinger, :gummy_bear,
:twizzler].include?(candy_params[:title].to_sym)
#candy = Candy.new(candy_params)
respond_to do |format|
if #candy.save
format.html { redirect_to #candy, notice: 'Candy was successfully created.' }
format.json { render :show, status: :created, location: #candy }
else
format.html { render :new }
format.json { render json: #candy.errors, status: :unprocessable_entity }
end
end
end
end
A couple of things,
First, params doesn't have :title, :title is in params[:candy][:title], or you just use candy_params[:title]
Second, the if statement could be shorter
if candy_params[:title] && !candy_params[:title].empty? && [:kit_kat, :skittles, :m_and_ms, :herseys_kiss, :butterfinger, :gummy_bear,
:twizzler].include?(candy_params[:title].to_sym)
(Go on and create the candy)
else
(Redirect with error messages | Wrong Candy Type)
end
It's always good to check the existence of the params and make sure it's not empty first, then check if it's included in the acceptable list. Notice that your original code was to compare symbol with string, so cast them to the same type and check.
UPDATE
Added else statement for redirect when :title isn't present, empty string, or wrong type

Fetching JSON Data from Facebook Graph URL in Rails

i have a ruby on rails 3.2.13 app, and i want to fetch data from my facebook page and show it one of my views...
What steps should i follow to do this?
What has to go in my controller and model and also in my view?
Please Help! ive been investigating on how to do this for like a week now, and i cannot find a good tutorial on how to get this done.
I have made a Data scaffold with the stuff i want to read from facebook.
This is my controller
class DatosController < ApplicationController
# GET /datos
# GET /datos.json
def index
#datos = JSON.parse("http://graph.facebook.com/iscopeapp")
respond_to do |format|
format.html # index.html.erb
format.json { render json: #datos }
end
end
# GET /datos/1
# GET /datos/1.json
def show
#dato = Dato.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #dato }
end
end
# GET /datos/new
# GET /datos/new.json
def new
#dato = Dato.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #dato }
end
end
# GET /datos/1/edit
def edit
#dato = Dato.find(params[:id])
end
# POST /datos
# POST /datos.json
def create
#dato = Dato.new(params[:dato])
respond_to do |format|
if #dato.save
format.html { redirect_to #dato, notice: 'Dato was successfully created.' }
format.json { render json: #dato, status: :created, location: #dato }
else
format.html { render action: "new" }
format.json { render json: #dato.errors, status: :unprocessable_entity }
end
end
end
# PUT /datos/1
# PUT /datos/1.json
def update
#dato = Dato.find(params[:id])
respond_to do |format|
if #dato.update_attributes(params[:dato])
format.html { redirect_to #dato, notice: 'Dato was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #dato.errors, status: :unprocessable_entity }
end
end
end
# DELETE /datos/1
# DELETE /datos/1.json
def destroy
#dato = Dato.find(params[:id])
#dato.destroy
respond_to do |format|
format.html { redirect_to datos_url }
format.json { head :no_content }
end
end
end
This is my Model
class Dato < ActiveRecord::Base
attr_accessible :likes, :name, :talking_about_count
end
this is the facebook json link i want to parse and show some fields of it in my view.
When i access the index of this controller im getting an error : "Unexpected Token at:http://graph.facebook.com/iscopeapp"
http://graph.facebook.com/iscopeapp
Please help!
Thank you in advance!
You could use Koala:
Koala is a Facebook library for Ruby, supporting the Graph API (including the batch requests and photo uploads), the REST API, realtime updates, test users, and OAuth validation

Why Doesn't Rails Scaffold Include Json Rendering for Edit in Controller?

This is just out of curiosity, here's a generated controller from running rails g scaffold Thing:
class ThingsController < ApplicationController
# GET /things
# GET /things.json
def index
#things = Thing.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #things }
end
end
# GET /things/1
# GET /things/1.json
def show
#thing = Thing.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #thing }
end
end
# GET /things/new
# GET /things/new.json
def new
#thing = Thing.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #thing }
end
end
# GET /things/1/edit
def edit
#thing = Thing.find(params[:id])
end
# POST /things
# POST /things.json
def create
#thing = Thing.new(params[:thing])
respond_to do |format|
if #thing.save
format.html { redirect_to #thing, notice: 'Thing was successfully created.' }
format.json { render json: #thing, status: :created, location: #thing }
else
format.html { render action: "new" }
format.json { render json: #thing.errors, status: :unprocessable_entity }
end
end
end
# PUT /things/1
# PUT /things/1.json
def update
#thing = Thing.find(params[:id])
respond_to do |format|
if #thing.update_attributes(params[:thing])
format.html { redirect_to #thing, notice: 'Thing was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #thing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /things/1
# DELETE /things/1.json
def destroy
#thing = Thing.find(params[:id])
#thing.destroy
respond_to do |format|
format.html { redirect_to things_url }
format.json { head :no_content }
end
end
end
Rails includes a format block in every action except for edit... Why is this? Theoretically another app pinging the server for json would still want to show whatever is being edited, right? It's easy enough to just add in, but I am curious why they chose to do it this way.
If you want to know what you are updating, you can do it via the show action.

Rails before_filter doesn't hit database

In my application, I want to only allow user with admin privilege to access this model. So I set up and before_filter to check if the user is an Admin. The problem with this approach is that, after the admin user passes the filter, s/he won't be able to get redirect to the action. Instead, only the views are rendered, which leads to the undefined method each' for nil:NilClass error. What am I doing wrong here?
class TidbitsController < ApplicationController
before_filter :is_admin?
layout "tidbits"
# GET /tidbits
# GET /tidbits.json
protected
def is_admin?
unless current_user.admin?
flash[:error] = "You are not authorized!"
redirect_to "/" and return
end
end
def index
#tidbits = Tidbit.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #tidbits }
end
end
# GET /tidbits/1
# GET /tidbits/1.json
def show
#tidbit = Tidbit.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #tidbit }
end
end
# GET /tidbits/new
# GET /tidbits/new.json
def new
#tidbit = Tidbit.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #tidbit }
end
end
# GET /tidbits/1/edit
def edit
#tidbit = Tidbit.find(params[:id])
end
# POST /tidbits
# POST /tidbits.json
def create
#tidbit = Tidbit.new(params[:tidbit])
respond_to do |format|
if #tidbit.save
format.html { redirect_to #tidbit, notice: 'Tidbit was successfully created.' }
format.json { render json: #tidbit, status: :created, location: #tidbit }
else
format.html { render action: "new" }
format.json { render json: #tidbit.errors, status: :unprocessable_entity }
end
end
end
# PUT /tidbits/1
# PUT /tidbits/1.json
def update
#tidbit = Tidbit.find(params[:id])
respond_to do |format|
if #tidbit.update_attributes(params[:tidbit])
format.html { redirect_to #tidbit, notice: 'Tidbit was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #tidbit.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tidbits/1
# DELETE /tidbits/1.json
def destroy
#tidbit = Tidbit.find(params[:id])
#tidbit.destroy
respond_to do |format|
format.html { redirect_to tidbits_url }
format.json { head :no_content }
end
end
end
in your example all your action methods are protected so maybe that's the problem?
I think you forgot to add the devise required callback filter
before_filter :authenticate_user!
before_filter :is_admin?

Resources