missing a template for this request format and variant - ruby-on-rails

I am new to Ruby on Rails and am trying to gain a strong understanding of how MVC works.
I did the following:
rails new bubblesman
rails generate controller bubble
in my bubble controller I created a method as follows:
def available
puts "YEP!!!!!!"
end
I put the following in my routes file:
'welcome' => 'bubble#available'
I navigate to http://localhost:3000/welcome
I get the below error:
ActionController::UnknownFormat (BubbleController#available is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not… nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.):
what I also don't understand is if I put this in my helper controller instead of my main controller it all works fine.

you need to create the available.html.erb file within the views/bubble/ directory. When the route takes you to that action, it also navigates you to that view, so if you put:
<h2>YEP!!!!</h2>
as the only line in that file, it should return that to you on the webpage.
In the future, you could use rails g scaffold bubbles and that will create a majority of the files (MVC) and routes for you.

Related

Rails API Versioning Spree + rabl

I've an e-commerce app which built using Spree, now i need to create a new version of the API for a certain controller and here's what i did
created a new controller in /api/v2/x_controller.rb
made an action index
add route for my controller in routes.rb
made a rabl file ( index.v2.rabl )
when i try to access the api my request hit the action but i keep getting error
ActionView::MissingTemplate (Missing template spree/api/v2/x/index)
i there any missing configuration i should do ?!
Your RABL view should be under
app/views/api/v2/x/index.rabl
There is a naming convention for views in Rails. Typically, the views share their name with the associated controller action. For example, the index controller action of the articles_controller.rb will use the index.html.erb view file in the app/views/articles directory. The complete HTML returned to the client is composed of a combination of this ERB file, a layout template that wraps it, and all the partials that the view may reference.

Missing template error for specific function – others work fine

I have a route that is triggered when someone clicks a hyperlink to download a file.
My routes:
'/document/download_some_file/' => 'documents#download_some_file'
My controller:
def download_some_file
content = 'hello world'
send_data content, :filename => 'some_file.dat'
# if I comment out the above line (send_data) I get a missing template error
# if I name this function anything other than download_xyz I get a missing template error
end
This works fine. However I have another hyperlink:
'/document/refresh_files/' => 'documents#refresh_files'
then
def refresh_files
#stuff here
#this throws a missing template error
#if I rename this to download_xyz it works fine
end
So...what's going on here exactly? The first function I showed (download_some_file) does work properly.
The refresh_files is what I'm trying to fix. It should just call another function inside the Documents controller. Even if I just do a puts I get a template error.
It depends on what you are doing with the route. Since you are sending a file in the download_some_file method, it does not require a template.
For refresh_files I am assuming you are not trying to send a file, but maybe refresh a list. Maybe you want javascript back. This will require a template named refresh_file.js.erb that lives in your app/views/documents folder. Any format that you are requesting will probably end up needing a template besides maybe json if you send it from the method itself, but you could build a template for that too.
Once you have a template that you want back from the controller in your folder that goes with your method, you will be able to respond to it
You need to have corresponding template in view called refresh_files.html.erb Controller method searches template with corresponding name.

error on rails 3

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.

Newbie Question:Have to type localhost/controller/index in browser to get index method to display in Rails. Why?

I am new to Rails and I am reading Sitepoint's Simply Rails 2 but I am using Rails 3.0.0. In the book we had just set up our first controller (StoriesController) and fired up the server and typed in http://localhost:3000/stories and it should have displayed the index.html.erb file but instead when I type in that url I get "Routing Error: No route matches "/stories" but when I type in http://localhost:3000/stories/index it works properly. Can somebody explain to me why rails is not loading the index.html.erb file implicitly when I go to localhost/stories?
Depending on how you created your routes (in config\routes.rb). Unfortunately, if you scaffold a controller, rails now generates a route like this:
get 'posts#index'
If it is a restful-controller, you better write
resources :posts
Or if it is a special controller (with only an index action) you could write
match '/posts' => 'posts#index'
To provide the fallback match ':controller(/action(/:id(.:format))) is generally avoided. Because it opens up all your controller-methods. The preferred way is that you declare explicitly how to access your site.

In Rails how can I get the contents of a page without making an HTTP request?

This may be really obvious, but in rails how can I get the contents of a page without making an HTTP request?
i.e. I want to grab the contents of any page that my rails app generates, in a script on the same server. This is of course trivial with an HTTP request, but how can I get the contents of a page otherwise?
This is what I am doing now which works fine:
require 'open-uri'
contents = open("http://localhost/path_to_my_page").read # but I really want to get rid of the overhead of the HTTP request
I need to be able to do this from a script, within the rails app, not a controller (e.g. with render_to_string)
This is how you get the contents of a page in the console. It might work for you:
require 'action_controller/integration'
app = ActionController::Integration::Session.new;
app.get('/path_to_your_page')
puts app.response.body
In your controller , you can call render_to_string instead of render and the page is returned instead of being send to the browser.
The arguments of both methods are the same, so you need to specify which controller and action you require to render.
Why not use erb directly? See this question for more details

Resources