I'm doing a Rails application with gem 'kaminari'.
Like in the given screenshot below,
all links in generated pagination starts with
localhost:5000
but i need to modify it and change,
localhost:5000 to something like => https://new_domain and the rest of the url to follow....
Or set the HOST like in default_url_options.
Please help!
If you want customize look in kaminari first generate pages which kaminari. Run below generator
rails g kaminari:views default
then edit the partials in your app's app/views/kaminari/ directory.
Related
My site is both available as:
www.site.com
site.com
I want the www.site.com to be the primary site. In case a user visits the naked site the header should have a canonical link to the www site. Is there any easy way to do this in Rails? I should be done for all pages from a certain controller. (not all controllers since the site is multi tenant through subdomain: like client1.site.com etc...)
Note: This comment has been heavily edited upon clarification from the original poster
To set a canonical link in Rails, you can use the canonical-rails gem: https://github.com/jumph4x/canonical-rails
Install the gem and then you can do:
rails g canonical_rails:install
Which will put a canonical_rails.rb file into config/initializers. Open that file and define your canonical url by adding this line:
config.host = 'www.yourapp.com'
Then, if you have an app/views/layouts/layout.html.erb file or similar, you can add:
<%= canonical_tag -%>
to the <head> portion of your view.
I have a rails engine, which is mounted in dummy/config/routes.rb using
mount Handicap::Engine => "/handicap"
In the engine, I have a number of controllers, and when I start a rails server in the dummy directory these routes are active e.g. /handicap/tees/index responds. However, when I go to /rails/routes it only shows:
handicap_path /handicap Handicap::Engine
rails_routes_path GET /rails/routes(.:format) sextant/routes#index
sextant_engine_path /sextant Sextant::Engine
I can list them using rake routes, but my normal work flow is to list them in the browser. How do I list the engine routes in the browser?
If you only want to show your routes in the browser in development mode, there's a rails page which you can call:
http://localhost:3000/rails/info/routes (available since Rails 4)
If you're upgrading from rails 3, you can remove the sextant gem from your gems as this is now part of the rails core.
If you want to show your routes in production to the user, you can implement it like the following: (implemented in bin/rake routes (here) you can call the same things from your code:)
Attempt 1:
Controller code:
# app/controllers/example_controller.rb
routes = Rails.application.routes.routes
#inspector = ActionDispatch::Routing::RoutesInspector.new(routes)
View Code:
# app/views/example/show.html.erb
# Yeah! There's also a HTML Table Formatter already to print routes in html
inspector.format(ActionDispatch::Routing::HtmlTableFormatter.new(self))
Attempt 2:
Do this in a helper:
# app/helpers/route_printing_helper.rb
module RoutePrintingHelper
def print_routes(view)
routes = Rails.application.routes.routes
inspector = ActionDispatch::Routing::RoutesInspector.new(routes)
inspector.format(ActionDispatch::Routing::HtmlTableFormatter.new(view))
end
end
And then call it:
# app/views/example/show.html.erb
print_routes(self)
Attempt 3:
This is the "cheapest" way of doing this:
# app/controllers/example_controller.rb
#routes_output = `#{Rails.root}/bin/rake routes`
Your view:
# app/views/example/show.html.erb
<pre><%= #routes_output %></pre>
I worked this out, the clue was the line:
sextant_engine_path /sextant Sextant::Engine
This indicated that I still had the sextant gem in my Gemfile. Sextant was the way of doing this in rails 3, and as noted by siegy22, it has not been needed since rails 4. The solution was simply to take sextant out of the gemfile. Given the project is on rails 5, it was about time!
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.
I am reading the documentation for it but still can't quite figure out how to set it up.
So far I created a jsroutes.rb file and gem in the Gemfile and declaring it in application.js file.
But now how to use it? What to do with jsroutes.rb file? Can someone show me how to use this?
https://github.com/railsware/js-routes
JS routes adds named routes to your javascript. you know how in view you can write new_blog_comment_path(#blog) and it automatically gives you /blogs/:blog_id/comments/new path? JS routes allows you to do same. So assuming you have blog and nested comments route, then you can use js-routes in your js: you can write Routes.new_blog_comment_path(blog_id_or_blog_json) and it will automatically generates string which is the path for that route.
i just installed the rails_admin admin gem to my existing rails app. Now i am wondering how do i add redcloth and/or ckeditor to it. When i add ckedtor, it just shows the description area and does not show the title and other fields. Also it does not format after saving. How do i edit the necessary files ?
Thanks
If it doesn't show buttons, it isn't the CKEditor, but the default text area, meaning insert CKEditor failed.
Things to check:
Make sure you set it on the fields, like so:
https://github.com/sferik/rails_admin/wiki/Text
Make sure you are using the correct gem version for your Rails (<3, >=3, >=3.2)
https://github.com/galetahub/ckeditor
Make sure you ran the generator, e.g.:
rails g ckeditor:install --orm=active_record --backend=carrierwave
Finally, check your browser console for errors.