Rails API Versioning Spree + rabl - ruby-on-rails

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.

Related

Rails generate controller command not building view files

I have been struggling to understand why I can't get view files created for my Rails project. The documentation shows that generating a controller also generates an associated view:
Rails Docs
If I use that same command, I get one file generated, being the controller.
My command
The only two files within the Views folder in my Rails project are mailer.html.erb and mailer.text.erb
Am I doing something wrong? Do I manually create the view files, and if so I can't seem to get them "connected" to the associated controller. I am new to Rails, so any insight would be helpful. Thanks!
try this command :
rails g controller Articles index create
You can create methods and views dynamically after If you typed controller name in command.
you are creating only a controller in your command not any methods that's why the views are not generated. If you need to generate views for the particular methods then you may run the below listed command.
rails g controller Articles index show
Here, index, show are the methods name. So, this command will create ArticlesController and also the respective view files.
You can use the scaffold option.
rails g scaffold Post name:string title:string content:text
This command will generate the following files.
File
Purpose
db/migrate/20100207214725_create_posts.rb
Migration to create the posts table in your database (your name will include a different timestamp)
app/models/post.rb
The Post model
test/unit/post_test.rb
Unit testing harness for the posts model
test/fixtures/posts.yml
Sample posts for use in testing
config/routes.rb
Edited to include routing information for posts
app/controllers/posts_controller.rb
The Posts controller
app/views/posts/index.html.erb
A view to display an index of all posts
app/views/posts/edit.html.erb
A view to edit an existing post
app/views/posts/show.html.erb
A view to display a single post
app/views/posts/new.html.erb
A view to create a new post
app/views/posts/_form.html.erb
A partial to control the overall look and feel of the form used in edit and new views
test/functional/posts_controller_test.rb
Functional testing harness for the posts controller
app/helpers/posts_helper.rb
Helper functions to be used from the post views
test/unit/helpers/posts_helper_test.rb
Unit testing harness for the posts helper
app/assets/javascripts/posts.js.coffee
CoffeeScript for the posts controller
app/assets/stylesheets/posts.css.scss
Cascading style sheet for the posts controller
app/assets/stylesheets/scaffolds.css.scss
Cascading style sheet to make the scaffolded views look better

Does the application controller have an associated template?

Suppose I run
rails new proj1
I can do
rails generate controller abc def
and that creates among other things, .\app\views\abc\def.html.erb
And I can edit config.rb to say
get '/', to: 'abc#def'
or
root 'abc#def'
And that will load that def.html.erb template when I go to http://127.0.0.1:3000/
But I'm interested if I can do that without creating that new controller. I'm interested in whether I can go to the template just using the application controller.
So, for example, I can edit .\app\controllers\application_controller.rb and add
def a
end
then does the application controller behave like other controllers and try to render a corresponding .html.erb file e.g. the 'a' action would try to render a.html.erb? If so, I can't find where I should place the a.html.erb file?
Other controllers have a subdirectory for them within views, and files within that subdirectory for each action, and one can do them manually or by using e.g. rails generate controller blah a b c to generate the controller with actions and with a template for each action.
But I can't see that for the application controller.. I can't see where its views are.
You can do it like this:
routes.rb:
get '/', to: "application#root"
application_controller.rb
def root
end
app/views/application/root.html.erb
hello world
Running the server and visiting localhost:3000 prints "hello world"
Sinatra does things similar to this. Routes and controller actions are basically combined and it'd be up to you to implement rails-like controllers yourself if you wanted them.

missing a template for this request format and variant

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.

How to Include a Helper Method from a Rails Engine (Spree) inside of another Rails Engine (AlchemyCMS)

What I've run into is this:
AlchemyCMS is a Rails Engine for allowing Rails applications to have a Content Management System. It also has a preview page where it can load up an iframe of the example page with the layout. The layout here is the Spree layout. I've modified Alchemy to be able to load up the spree application layout and not its default.
In doing so, it is not loading up the helper methods. I am currently receiving:
undefined local variable or method `title' for #<#<Class:0x007f8dcc359498>:0x007f8de17dd6a8>
Where title is the first helper method in the application.
I've tried 5000 different techniques to try to load in Spree's helper methods into AlchemyCMS and I just can't do it.
Does anyone know how?
Ben,
You could do so by either including Spree's helpers within your application controller or within the base Alchemy controllers.
There is an extension for alchemy and spree together, which does a similar thing here:
https://github.com/magiclabs/alchemy_spree/blob/master/app/controllers/spree/base_controller_decorator.rb
You will just want to go in the opposite direction so instead of decorating a Spree controller to add Alchemy in you would decorate Alchemy controllers to include whichever of Spree's controller helpers you need to use:
https://github.com/spree/spree/blob/master/core/app/controllers/spree/base_controller.rb
In this case you need to include the common controller helpers:
https://github.com/spree/spree/blob/master/core/lib/spree/core/controller_helpers/common.rb
EDIT:
Alchemy::BaseController.class_eval do
include Spree::Core::ControllerHelpers
include Spree::Core::ControllerHelpers::Store
helper Spree::Core::Engine.helpers
end

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.

Resources