Rails finding by params - ruby-on-rails

I'm building a API and want the show page for a user to be found by 'uid' instead of the record ID
I have this in my controller
def show
respond_with User.find_by_uid(params[:uid])
end
When I go to localhost/api/v1/users/8888888 Its returns "Null"
Finding by ID seems to work fine, am I doing something wrong here?
I tried put this in the rails console and it worked
User.find_by_uid("8888888")
I'm new to rails
Thanks

have you tried visiting:
localhost/api/v1/users?uid= 8888888 instead of the url you are using currently, except you are handling that correctly rails would have no knowledge of the uid param alternatively you could add this to your config/routes.rb file
get 'users/:uid', to: 'users#show'
With the hope that your controller is called UsersController then you can call localhost/api/v1/users/8888888 in your browser and it should behave as expected

Rather than just giving you the answer, I'll provide a tip on debugging Ruby applications (including Rails).
Get the pry gem and the pry-debugger gem and include them in your Rails application (there's plenty of posts around Google on how to include pry and pry-debugger in Rails).
put 'binding.pry' (without the quotes) at the beginning of your show method. In the console where your server runs, when show gets executed, execution will halt/pause at binding.pry. Type the following in the pry console to see what is available in the rails params hash.
pry> params
(this will print out the contents of params)
I would start my troubleshooting here, and post the contents of params and any relevant server logging here if you still can't figure it out.
edit
I don't have enough rep to comment, yet. Only really been on this site and using it a day or two.

Related

NoMethodError: undefined method 'login_path' in Ruby gem shopify_app

I am using the Gem shopify_app (https://github.com/Shopify/shopify_app) to build an embedded Shopify app. In the file login_protection.rb there is this method:
def redirect_to_login
session[:return_to] = request.fullpath if request.
redirect_to login_path(shop: params[:shop])
end
I am getting the above error message, that login_path is not found. Since nobody else is complaining about this, I'm almost certain I am missing something very simple but I really am stuck. Does anybody have any pointers to help me out?
Try running
rake routes | grep login
This should tell you what paths you have defined that contain login in them. Otherwise just run rake routes and ensure that login_path, or whatever path you are looking for exists.
Also you may have a syntax error here:
if request.
You are calling no method on request.
The problem was that in version 5.0.1 or 5.0.2 (I was upgrading from 5.0.0) a named route called login_route was introduced. Usually apps have this route, either explicitly named or derived by Rails from the URL /login. I instead used /shopify/login as URL to my login page so my derived route name was shopify_login instead. All in all I've been looking in all the wrong places and didn't the forest for the trees so I was stuck. Just make sure this named route login exists and everything is good.

rails current_teacher keyword not set

I'm new to Ruby on Rails and I'm looking at an application that has a variable called current_teacher. I cannot seem to find where this is set. Everywhere I look the code seems to read from it but where is it set. Is this one of those things that Rails does for you. There is a mode and a table called teachers, so I'm sure this has something to do with it.
I'm very confused by statements like the following, can someone tell me how Rails does this?
if current_teacher.can_request_fieldtrip
Suppose you have a controller like :
class ClientsController < ApplicationController
  def new
if current_teacher.can_request_fieldtrip
# code
end
  end
end
Here is debugging tips :
(a) put this in your Gemfile and do bundle install :
`gem 'pry-rails', :group => :development`
(b) Put the line binding.pry just before the if statement.
(c) Start rails server using rails s.
(d) Hit the browser like http://localhost:3000/new
(e) Now you will be in the Pry console. Just do in the console,
method(:current_teacher).source_location
And the above line tell you where the method has been defined.
Documentation of Method#source_location
Returns the Ruby source filename and line number containing this method or nil if this method was not defined in Ruby (i.e. native)
Rails does not support authentication by itself, however there are a lot of 'add-ons' that rails can use. These 'add-ons' are called gems. This can be a little confusing because you can't actually see their code inside your project folder.
If you open a file called "Gemfile" (it should be in your project folder) you can see a list of gems that you use. Try searching their names on google, you will probably find official web page that contains it's documentation. That way can learn what they do and how to use them.
current_teacher method smells like "Devise" gem
https://github.com/plataformatec/devise
I'm not sure about can_request_fieldtrip, this could be a custom method defined in Teacher model.

What are the routes I need to set up to preview emails using Rails 4.1 ActionMailer::Preview?

class UserPreview < ActionMailer::Preview
# Accessible from http://localhost:3000/rails/mailers/notifier/welcome_email
def welcome_email
UserMailer.welcome_email(User.first)
end
end
I have this simple mailer preview using Ruby on Rails 4.1.
If I comment out, all of the routes in my routes.rb file and leave only this, the mailer preview works:
MyTestApp::Application.routes.draw do
end
So obviously one of my rights is getting used before the default Rails one for mailer previews.
What do I need to type into the routes rb file?
I know this is an old question, but figured I'd post an answer anyway.
I'm guessing you have a route similar to this near the end of your routes.rb file:
match '/:controller(/:action(/:id))'
That is a 'catch all' route. The rails code appends the mailer preview routes to the end of the routes, so they are never reached due to the 'catch all' route.
It sounds like the 'catch all' route may be retired in rails 5.0? It is probably a good idea to review your routes so you don't need a 'catch all'. Here is a link to the issue where someone mentions the 'catch all' is being retired at some point: https://github.com/rails/rails/issues/15600
So, here is the fix. Use at your own risk!
Insert the mailer routes before your 'catch all'.
get '/rails/mailers' => "rails/mailers#index"
get '/rails/mailers/*path' => "rails/mailers#preview"
That will allow your mailers to work and your 'catch all' will continue working. Now, this is a complete hack which should only be used until you're able to fix the root issue, which is eliminating the need for the 'catch all' route.
I did find the following in the issues list for rails, which looks like has been accepted and merged. Not sure what version it is in, but it seems like they have updated the mailer preview code to prepend the routes instead of appending them.
https://github.com/rails/rails/pull/17896/files
Good luck!

Start Rails with Backbone.js

I amreading about the backbone.js and make a demo application for the CRUD operation from by goggling some links.
and see the structure of the backbone.js which initial created by installing the gem 'backbone-rails',
But here my query is like when we have normal application like without using of any .js, we have some options to check the flow of the application, like for example we can write 'exit' keyword in controller and check the methods which is calling by routes and can inspect and see all the records and parameters which fetched by model and parameters which input by user .
In using of the backbone.js, i am confusing that from where the view is coming and is there any way to see it's line by line flow means
first go to model and for query data can here we can show it in console and see what is going on.
For debug the backbone applications place 'debugger' in your js code and the js execution will stop there if you use chrome. In other browser I am not sure if works but in chrome I used this technique many times... And you can step line by line you can check variables and so on.
For debugging rails use these gems
gem 'pry'
gem 'pry-nav'
Then in your controller where you want to inspect the variables and the calling stack put binding.pry
Like here:
class UsersController <...
def index
binding.pry
#users = Users.all
end
end

Rails: how do you access RESTful helpers?

I'm trying to work through this guide to Rails routing, but I got stuck in section 3.3:
Creating a RESTful route will also make available a pile of helpers within your application
and then they list some helpers like photos_url, photos_path, etc.
My questions:
Where can I find the complete list of helpers that is "made available?"
Is there a way to call the helpers in the console? I created an app, then opened up the console with script/console. I tried to call one of the helpers on the console like this:
>> entries_url
But got:
NameError: undefined local variable or method `entries_url' for #<Object:0x349a4>
from (irb):8
You have several questions in there, most of which have already been answered by people below.
The answer to one that wasn't fully addressed however, is: yes you can use the script/console to see where your routes go. Just type in app.[route_helper] and it will respond with the path. For example app.users_path will return /users/
So for your example type app.entries_url for the full URL - or app.entries_path for its relative path within the console.
rake routes at the command line should get you that list.
I think this may be what you are looking for ... http://topfunky.com/clients/peepcode/REST-cheatsheet.pdf
You can access other helpers in the console by prepending "helper."; ie. helper.progress_box (assuming #progress_box exists of course)
From memory, you can't call url/path helpers from the console for some reason.

Resources