I have a model, controller and routes configured sucessfully with the name animegif.
In my show method, it looks under application/show instead of views/animegif/show.
Missing template animegif/show, application/show
"searched in /Users/myName/Desktop/testapp/app/views
My easy fix was adding this method to my animegif controller but I do not understand why is it not searching under views/animegif/show by default.
When I followed the rails tutorial by Michael Hartl, the paths were located correctly. Is there something I am doing wrongly?
Name of controller: animegifs_controller, model: animegif
For my routes I am using resources to generate the default routes for the model
def self.controller_path
"animegif"
end
If you read the error message once again you can see it first animegif/show directory of your views. If rails does not find the required template in that directory then it falls back to app/views. You are only getting this error message because either the show.html.erb is not there or you have any typo error.
If you are more curious about how rails finds your views, please refer to this post by Andy Wang. He has explained it in a better way.
Regarding the error message it is looking in app/views/animegif/ and app/views/application/ but cannot find the template. So you probably have a typo in the template's name show.html.erb (or other formats than html) in the respective directory.
Related
I'm a beginner to both ruby and rails, and using Rails 5.17 to develop a web app for a class.
Creating the empty Rails project was successful, but something is going wrong when creating a new controller. I generated a new controller named cars from the root of the project, which was successful. There was a file in app/controllers named cars_controller.rb which looks like this:
class CarsController < ApplicationController
end
I added a method to this file named hello that does nothing.
I then created a file named cars.html.erb in the app/views/layouts directory. This file is a basic page of html code.
In config/routes.rb, I added the following:
get '/cars', to:: 'cars_controller#hello'
resources: cars
After all of this, I ran rails server, and opened localhost:3000 in a browser. This brings up the normal Ruby on Rails welcome page.
But when I go to localhost:3000/cars, I get the following:
Routing Error
uninitialized constant CarsControllerController
I've tried changing the name of the cars_controller.rb file. I've tried changing the name of the class in the controller file from CarsController to Cars. I've tried many different routes in routes.rb. I finally tried uninstalling Rails 5.17 and installing Rails 5.13.
I'm very confused, and I'd be grateful for any advice I can get. Thanks in advance!
One of the great things about Rails is its preference for convention over configuration. However, for this to really benefit you, you need to stick to doing things “The Rails Way” rather than your own way, wherever possible.
In this case, start by getting rid of your custom get route, and just use resources :cars.
From the command line, run rake routes (you might be able to run rails routes on your rails version too) and see the routes that it has created for you.
Now, rename the method you added to your CarsController from hello to index.
Move your hello.html.erb file from app/views/layout to app/views/cars/index.html.erb.
Finally, start the rails server (rails start) and load the url http://localhost:3000/cars in your browser.
—-
Note that templates in app/views/layout have a special purpose. These are used to apply a general template to your views. Look up the use of layout within a controller for more details
I think you have an error in how you had defined your route - you don't need _controller.
Instead, try this:
get '/cars', to: 'cars#hello'
Also, keep in mind that in your cars directory you need the view: hello.html.erb
When I try to render a partial from a helper, it fails with this (condensed) error message:
Missing partial /_cube_icon with [...]. Searched in:
Note that the list of searched directories is empty!
In contrast, when using render in a view, it knows where to look:
Searched in: * "/Users/Lars/GitHub/algdb/app/views"
In the helper code, I use ActionController::Base.helpers.render(). Should I use some other render function? How do I tell it where to look for partials? Could I have set up the project wrong somehow?
This is Rails 4.2.4 · Ruby 2.3.1
OK, I figured it out:
I was calling render from code in the helper directory, but not from a function in a standard SomethingHelper module.
When following that convention, things started working.
Partial files normally reside within the app/views directory and are not located in the helpers directory in Rails as you can see from the error message you are receiving. To solve your problem, I would move the _cube_icon file into the app/views directory and organize your code there. I recommend reading this section of the Rails documentation for views Layouts and Rendering
Additionally, it sounds like you may be new to rails so I would take a look at the conventions that rails offers. Rails, as you may or may not know, is an opinionated framework which means certain things need to go in certain locations in order for it to work. Here is another resource on just the view part of Rail's MVC framework Action View Overview. Hope this helps.
---Updated-----
If you really want to render a partial from a helper file, there are a few ways to do so but the best one to use is outlined below:
In app/helpers
module MyHelper
def custom_render
concat(render(:partial => 'cube_icon'))
end
end
Here are some links from stackoverflow to help you out.
Rendering a partial from helper #1
Rendering from a partial from herlper #2
Using concat rails
I have added into application.rb string
config.paths['app/views'] << 'app/views/cabinet'
and created a view 'app/views/cabinet/index.html.slim'.
But when I go to route localhost:3000/manager/pages (It uses layout manager if it make sence), Rails gives the error
Manager::PagesController#index is missing a template for this request format and variant.
What I'm doing wrong?
I am not sure what you are trying to do by overriding the mapping for the location of app/views but it doesn't sound like a good idea to me.
Without knowing more about your code I would suggest you remove that config line from application.rb and simply use:
render 'cabinet/index'
from PagesController#index action.
Hi this is ROR beginner's question.
I have creat controller.rb and view hello.rhtml following the tutorial, but when I try to open localhost:3000/say/hello, it come up with with error: No route matches [GET] "/say/hello"
could any one advice please?
Well you need to setup a route for that in your config/routes.rb file.
For first try i would say use a script generator, enter on the command line as being in the project library > rails g controller helloworld index. This will create a route for itself, and a controller file.
After this script runs, there should be a line in your config/routes.rb
Cloud::Application.routes.draw do
get "helloworld/index"
end
Then you need to enter localhost:3000/helloworld/index in your browser url bar. Then ( as default ) rails will render the view located in app/views/helloworld/index.*. If you want to change this behaviour, go to the helloworld controller.
For more info there is a useful guide: ROUTING GUIDES
You need to define a route definition so that the URL you are requesting gets mapped to an action in the controller you have created which would render hello.rhtml. Say your controller name is says_controller.rb (thats how Rails gives the filename). In that if you define and action hello which would by default render hello.rhtml, then the fallback routes which are defined in the routes.rb file at the end would make a request to say/hello to look for the say_controller and hello action, thus rendering hello.rhtml.
For detailed help you can refer to the Rails Guides. There is a lot of helpful material and it is explained very well.
I started developing RoR recently and the best practise I got was the tutorial # rails for zombies and video's # railscasts. I suggest you watch some / make some and you get a general idea how to get started :)
-edit- on this issue: You're trying to render the hello view from the say controller.
since routing is handled by default on :controller/:action, do you have a action called hello in say? No action means no route means no view rendered.
class SayController < ApplicationController
def hello
#do nothing or add some code
logger.debug "I'm in the say controller, hello action!"
end
end
This should get it to render the hello file. You might want to take a look at restful actions / crud though, rails uses those by default.
I'm working on a ruby on rails project currently, doing it without scaffolding or anything just by hand. I have an index controller that simply lists all existing items in the db with a link on each that redirects to a details view.
The error I am getting is when trying to link to the item I have this line of code:
<%= link_to "Show", person %>
this is the same line of code I see everywhere, even in other working apps, I know person is the right variable name but no idea why this is failing. The error I get at runtime is:
undefined method `person_path' for #<ActionView::Base:0x7fe4d07f6568>
any helpful hints?
Check your "routes.rb" file. Make sure you have a declaration similar to:
map.resources :people
If you open terminal and run "rake routes", it will show you all routes that are currently recognized by your app.
Sounds like the route for Person is not configured properly. Is the app using REST-ful routing? Read more at: http://guides.rubyonrails.org/routing.html