Hanami routes helper don't work in templates - hanami

I have hanami application version 1.0.0
I have next routes.rb file:
get '/games', to: 'games#index'
root to: 'home#index'
I read docs and tried to use
<%= routes.games_path %>
in application.html.erb template, but recieved next error:
Hanami::Routing::InvalidRouteException: No route (path) could be generated for :games - please check given arguments
How can I use routers in hanami templates?

Instead of using resources, you can also add a name to your route:
get '/games', to: 'games#index', as: :games
Read more at: https://guides.hanamirb.org/routing/overview/

I added
resources :games, only: [:index]
to my routes.rb file and this fixed my problem.

Related

Getting routes.rb root to work in Rails

Started working with RoR again! I'm having trouble getting my config/routes.rb file to perform. I'm getting "uninitialized constant ApplicationsController" using RubyMine.
Here's what I have changed in my routes.rb after trying to search things down:
resources :applications
root :to => 'applications#index'
application_controller.rb has:
class ApplicationController < ActionController::Base
protect_from_forgery
end
I've ran my rake routes:
applications GET /applications(.:format) applications#index
POST /applications(.:format) applications#create
new_application GET /applications/new(.:format) applications#new
edit_application GET /applications/:id/edit(.:format) applications#edit
application GET /applications/:id(.:format) applications#show
PUT /applications/:id(.:format) applications#update
DELETE /applications/:id(.:format) applications#destroy
root / applications#index
rake rails:update and all changes were approved except altering routes.rb
Heres what its kicks out Rubymine side:
Started GET "/" for 127.0.0.1 at 2012-11-11 02:50:27 -0800
Connecting to database specified by database.yml
ActionController::RoutingError (uninitialized constant ApplicationsController):
Thanks for the help!
You should rename application_controller.rb to applications_controller.rb
Check if you have a file called applications_controller in your app/controller folder, or its name is application_controller, so rename it to applications_controller, if you don't have that file, then create your ApplicationsController:
rails generate controller applications

Rails 3 Doesn't Look In Public Folder For Assets On Show Method

In my show views whenever I try and display an image using the image_tag builder rails doesn't look for images in the public folder inside of my show views...
For instance:
<%= image_tag "thumbnails/fish.jpg" %>
Will produce this:
ActionController::RoutingError (No route matches [GET] "/uploads/thumbnails/fish.jpg"):
I'm using the paperclip Gem for my upload model and I'm saving uploads to a different folder than the public folder for security reasons, and yes, this show view does occur within the Upload controller...
In my Upload model I use this line to save uploads to a non-public folder:
has_attached_file :upload, :path => ":rails_root/:class/:id/:basename.:extension",
:url => ":rails_root/:class /:id/:basename.:extension"
Rake routes:
upload GET /uploads/:id(.:format) {:action=>"show", :controller=>"uploads"}
PUT /uploads/:id(.:format) {:action=>"update", :controller=>"uploads"}
DELETE /uploads/:id(.:format) {:action=>"destroy", :controller=>"uploads"}
/download/:id(.:format) {:controller=>"uploads", :action=>"download"}
Edit
Note: If I explicitly make an img tag and point the src to my image it works fine on my show views so I don't think it's a permission issue.
The answer was fairly simple, and I can't believe Rails is being this picky but I needed to include a forward slash at the beginning of the path as so:
"thumbnails/fish.jpg"
becomes
"/thumbnails/fish.jpg"
I'm still curious as to why this is only a problem on non-index views...
You may need to change this line to true:
config.serve_static_assets = false
in the environment/*.rb file (e.g. development.rb).
Or I have seen where this is a permissions issue on the directory in question so a CHMOD 777 on the directory may resolve it/point you in the right direction.
Here is an SO ticket on a similar issue

How do I add csrf_meta_tags to my .coffee.erb file?

I have a .coffeescript.erb file in which I would like to get CSRF meta-tag information in Rails 3.1. The file is called bookmarklet.coffee.erb and it is in my assets/javascript folder. This is the code I am using:
csrfMeta = <%= csrf_meta_tags %>
When I access assets/bookmarklet.js, I see this in my server log
Error compiling asset bookmarklet.js:
NameError: undefined local variable or method `csrf_meta_tags' for #<#<Class:0x007f83d2efc3a8>:0x007f83d4043080>
How can I get the CSRF meta tags into my javascript file?
I managed to get the CSRF tags to show up by moving the file into my views/parser folder, and renaming it as bookmarklet.js.coffee. Instead of using csrf_meta_tags, I used the helper form_tag which automatically includes the CSRF tags.

Omniauth can't find route

I'm trying to implement Omniauth with my Rails 3 app. I followed the tutorial from the Railscast episod 205, but can't get it to work. When I call the '/auth/twitter' (it doesn't work with any provider) Rails complains it can't find the appropriate route (it tries to load my default route).
I added omniauth.rb under config/initializers/, put gem 'omniauth' in my Gemfile, and ran bundle install.
I'm not sure how to debug this problem nor what information to provide to help understand the problem.
After some research I found a solution. I added the following to my routes.rb file:
get 'auth/:provider' => 'authentications#passthru'
and to my authentications controller:
def passthru
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
Not sure why I had to do that, but it's working...
Here were my steps to get it working:
1) Add omniauth to your Gemfile
2) Add omniauth strategy to your Gemfile (omniauth-linkedin for example)
3) Run bundle install
4) Add the omniauth initializer in config/initializers/omniauth.rb
5) Add the match auth/:provider/callback => users#omniauth route to routes.rb (point it to the controller/action where you'll handle the authentication response)
6) Build the action to handle the authentication response in the controller you referenced in step #4
7) Restart your web server
The default /auth/:provider route wasn't recognized for me until I restarted Apache.
You have to add 'omniauth-twitter' to your gemfile, and
:strategy_class => OmniAuth::Strategies::Twitter
to your omniauth.rb, at the end of the twitter provider.
Then it will recognize the /auth/twitter path.

map.root does not work in ruby on rails

I have deleted public/index.html.
Here is my config/routes.rb
Those all I have in that file
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
map.root :controller => "home"
I've also tried using map.root :controller => "home#index"
when I ran rake to check
$ rake routes
(in /var/www/atlantix)
/:controller/:action/:id
/:controller/:action/:id.:format
root / {:controller=>"home", :action=>"index"}
I also have:
app/views/home/index.html.erb
app/controllers/home_controller.rb
My issue:
I am a complete newbie of ROR.
I am using Apache and Ruby 1.8.
When I navigate to the http://localhost/myapp/,
I do not see the new home page I created.
Am I missnig something critical in my setup and configuration.
Please advise & help
Mmmm you seem to mix Rails 2 syntax. In Rails 3 you should write:
root :to => 'home#index'
Note: you should also delete the index.html from the public folder.
More information concerning routing can be found here.