I am doing Hartl's tutorial. I often don't know whether some methods mentioned in the tutorial are defined in Rails or in the code in the tutorial. I have problem finding methods when learning Rails. Does anyone know where I could find the API for:
edit_account_activation_url(#user.activation_token, email: #user.email
in Listing 11.13? Can someone advise me how I could finding the methods' API in general?
It is url helper, it's automatically created from routes.rb. You can find more in Rails guides
In routes you have a line resources :account_activations, only: [:edit]
According to url generation rules it produces 1 route:
http://www.example.com/account_activations/:id/edit
Helper edit_account_activation_url takes id as a first parameter, it is #user.activation_token in your case. As a second parameter it takes query hash (we can omit {} for last hash parameter) - it is { email: #user.email }. That how it works
You can solve the problem of "not finding the methods' API" by reading the guide together with the tutorial
Can someone advise me how I could finding the methods' API in general?
Good question. I use Pry.
[3] pry(main)> show-source app.users_path
From: /home/sergio/.gem/ruby/2.4.4/gems/actionpack-4.2.11/lib/action_dispatch/routing/route_set.rb # line 342:
Owner: #<Module:0x0000563dd616fc28>
Visibility: public
Number of lines: 5
define_method(name) do |*args|
options = nil
options = args.pop if args.last.is_a? Hash
helper.call self, args, options
end
In this particular case, it's much less helpful than the routing guide mentioned by #Vasilisa. But at least it gives you a hint. And it's very useful generally. Especially when you're dealing with, say, a gem/library which uses inheritance/mixins heavily and you're never quite sure which of 30 call implementations you're invoking here. This approach shows exact location and source code.
A good source of information is APIdock. This site allows you to do a search against Ruby or Rails for API information. What is useful is that users will add additional code snippets or comments about the methods on the API
The other source of just Rails API is at Ruby on Rails API. This typically gives you the latest API information. Comparatively the APIdock site will show the different Rails release version on the version timeline at the top of the page
Related
When I generate a scaffold in rails, I noticed that the methods are prefaced with # remarks. I haven't been able to find any documentation as to if they actually do anything of if they are similar to what looks like remarks in application.js that look like remarks but are really code.
For example:
# POST /attachments
# POST /attachments.json
def create
and
# GET /attachments/1
# GET /attachments/1.json
def show
end
I am using rubymine as my editor.
No, they don't do any magic behind the scenes. They are just comments to help you out.
By default, scaffolding will direct POST requests to create(), and GET of a particular resource (e.g. /resources/<id>) to show(). These associations are defined in your routes, and scaffolding applies this convention. You're free to change them in your routes if you wish.
Those are just comments in Ruby. They are ignored by the Ruby interpreter and are meant for the developer.
I'm trying to get dynamic_sitemaps gem to work with my site, but the readme is very technical and a bit over my head at the moment.
I'm running into errors when trying to generate the sitemap for this bit of code.
# You can have multiple sitemaps like the above – just make sure their names are different.
# Automatically link to all pages using the routes specified
# using "resources :pages" in config/routes.rb. This will also
# automatically set <lastmod> to the date and time in page.updated_at:
#
sitemap_for :offers
It's returning the below error
ArgumentError: The collection given to sitemap_for must respond to
find_each. This is for performance. Use Model.scoped to get an ActiveRecord relation that responds to find_each.
I'm looking to have the sitemap contain all my offer posts etc.
Any help is greatly appreciated!
If your model's name is Offer, try
sitemap_for Offer.all
(note: #scoped is deprecated, so #all seems to be the better option going forward)
I'm sauntering through Michael Hartl's Rails Tutorial right now, and am finding that I'm constantly encouraged to use wonderful methods that inexplicably do amazing things. He does a generally competent job of explaining what they do, but there is no real nitty gritty of why and how they work.
Specifically, I have just been plundering the rspec gem on github searching for the source code to the "describe" method. I cannot find it. Having now read a large amount of the source code (at an apprehension rate of about 25%) searching for it, I know that once found, I will need to look at its parent classes and modules to understand a certain amount of inheritance before I can really grasp (and then never let go of) the flesh and bones of "describe".
I don't mind struggling to grasp the concept, I'm a fan of attempting to read code in new languages before I fully understand it so that I can read it again later and use the comparison of my comprehension as a gauge of my fluency. I'd just like a kicker. Either a description or a file location with maybe a little helper hint to get me started.
For example...
I found this:
# RSpec.describe "something" do # << This describe method is defined in
# # << RSpec::Core::DSL, included in the
# # << global namespace (optional)
and rpsec/core/dsl states:
# DSL defines methods to group examples, most notably `describe`,
# and exposes them as class methods of {RSpec}. They can also be
# exposed globally (on `main` and instances of `Module`) through
# the {Configuration} option `expose_dsl_globally`.
but then there is no "class Describe" or def "describe" or such in that file.
SO: can anyone tell me where the "describe" method is, how it works, exactly, or (if not) why I am naively searching for the wrong thing in the wrong locations?
As you may know, there is no difference between describe and context methods and you can use them interchangably. Rspec developers could not let themselves to repeat the same code for different method names, so they moved the declaration to
module RSpec
module Core
class ExampleGroup
def self.define_example_group_method(name, metadata={})
# here they really define a method with given name using ruby metaprogramming
define_singleton_method(name) do |*args, &example_group_block|
And call that method a bit later for all the same-functionality DSL methods:
define_example_group_method :example_group
define_example_group_method :describe
define_example_group_method :context
So in case you are looking for describe method source, dive into define_example_group_method with assumption that name argument equals to describe and example_group_block is your block body.
The RSpec code base is not a trivial thing to get your head round. However, these links should get you started ...
This line defines the describe keyword:
https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/example_group.rb#L246
The method above that line does the heavy lifting for you. Take your time reading it.
This part then exposes the generated method:
https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/dsl.rb#L54
Good luck!
I'm trying to translate the routes in a Rails application. I tried with i18n_routes, but I had some problems with translating actions that didn't depend on a resource. In the end, I solved it without using i18n_routes, as explained here.
The issue now is that I don't find the way to translate scopes. This is my routes.rb:
scope "sport" do
translated_named_route 'it_is_healthy', 'sport#it_is_healthy'
translated_named_route 'it_is_social', 'sport#it_is_social'
end
How to translate sport? I tried with i18n_routes again, but I don't see how to translate the scope. Thanks!
If you are open to using a gem for this, I would recommend using
https://github.com/francesc/rails-translate-routes
The readme is fairly comprehensive and it definitely covers namespaces and scopes in the routes as well.
I'm trying to make a Rails application that serves simple static HTML pages. I followed Mikel's tutorial here (it involves making a Pages controller and setting up some routing) but I keep getting an error message.
I made a app/views/site/pages/_about.html.erb file to contain my About page. After starting the rails server, I try to go to http://localhost:3000/about/ but it gives me a Routing Error because I have an "uninitialized constant Site."
My project is uploaded to GitHub if you want to take a look at the code.
Edit: here's my config/routes.rb file:
NINAgallery::Application.routes.draw do
match ':page_name' => 'site/pages#show'
end
And here's the important part of my app/controllers/pages_controller.rb file:
class PagesController < ApplicationController
layout 'site'
def show
#page_name = params[:page_name].to_s.gsub(/\W/,'')
unless partial_exists?(#page_name)
render 'missing', :status => 404
end
end
# extra code for handling 404 errors goes here
end
site/pages#show means the show action in Site::PagesController
You either need to put your controller in the namespace your routes imply or change the route
The last line in the PagesController is this:
ValidPartials = Site::PagesController.find_partials
That means that the PagesController is contained in a Site module. But there is no Site module in your app.
I think simple removing Site:: should fix the problem:
ValidPartials = PagesController.find_partials
Plus the route:
match ':page_name' => 'pages#show'
Your application is called NINAgallery.
Replace Site in pages_controller.rb line 27 by NINAgallery.
PS:
I just took a peek at the so-called tutorial. You are taking really really really bad habits.
Some resources to take very good basics:
http://guides.rubyonrails.org/
http://api.rubyonrails.org/
If you like tutorials: http://ruby.railstutorial.org/
And there are plenty of books about rails. All good.
Besides the namespace problem, you also needed to add the 'app' Gem to the Gemfile, as explained in the tutorial.
I don't know why you removed the caching of the static pages in your working code. I made a pull request with the app working and maintaining the cache problem. If another person is interested, the code is here
Also ryan bates has a tutorial called "Semi static pages" that does something similar. I would encourage you to follow his solutions because there are very rarely mistaken.