undefined method 'patch' - routing error - ruby-on-rails

I am beginner in Rails & MVC development, and need help with the below:
I am doing the example in http://edgeguides.rubyonrails.org/getting_started.html
In "5.12 Updating Posts", we are asked to add the following to our config/routes.rb:
patch "posts/:id" => "posts#update"
If I do that, and run rake routes, I am getting the below error:
undefined method `patch' for #<ActionDispatch::Routing::Mapper:0x390f078>
I get the same error when I go to - http://localhost:3000/posts/1
This is the line in edit.html.erb :
<%= form_for :post, url: { action: :update, id: #post.id }, method: :patch do |f| %>
I have Rails 3.2.1.
Environment:
I am doing this in Windows 7. I installed Rails via railsinstaller.org. Browsers - Chrome, Firefox

patch is only available in Rails master branch.
Here is the related pull request: https://github.com/rails/rails/issues/348
Here is the blog article explaining the reasoning: http://weblog.rubyonrails.org/2012/2/25/edge-rails-patch-is-the-new-primary-http-method-for-updates/
Here is another great summary: http://blog.remarkablelabs.com/2012/12/http-patch-verb-rails-4-countdown-to-2013
Among other things, you need to point to the git repo in your Gemfile to use edge rails on an already-existing project.
gem 'rails', :git => 'git://github.com/rails/rails.git'
For now you should just use PUT instead of PATCH. Even when 4.0 comes out, PUT isn't going anywhere.

fwiw, I backported the HTTP PATCH verb work for Rails 3.2 https://gist.github.com/bf4/8940203

Related

Ruby on Rails API for beginner json response

I am new to ruby on rails. I apologize early if this question might be dumb... but I can't figure this out and the tutorials I've seen is making this simple task confusing. Or I am just not understanding what I am missing or what to do... if a step by step tutorial may help, thanks!
My task to make a rails api that you can pass JSON to via postman/DH.
The payload will be {"value": "foo"}. A '200 (OK)' Response with payload of {"value":"bar"}
No database needed, just a controller that takes in a payload and gives back a response.
This is what I do know...
What I do know is how to create a rails api by "rails new testrails --api"
Then going to my 'gemfile' and installing some kind of json to gems.
Then... I mess with the 'route.rb' file in config and the controller in'apps'.
I get lost here on what I need to do with the route file and controller...
What ever steps come next.
Step 1: install Rails 5
$ gem install rails
Step 2: create an API-only Rails application
$ rails new my_app --api
Step 3: integrate rack-cors
Add gem 'rack-cors' at the bottom of your $RAILS_ROOT/Gemfile ($RAILS_ROOT stands for the root directory of your rails app), then
$ bundle install
Then add these lines to your $RAILS_ROOT/config/application.rb, inside the class definition
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :patch, :put, :delete, :options]
end
end
Step 4: generate a controller
$ rails g controller foo
Add an action to the FooController (in file $RAILS_ROOT/controllers/foo_controller.rb)
def create
foo = params[:foo]
# Do whatever you want with foo
render json: {value: 'bar'}
end
Step 5: add a route
Modify the file $RAILS_ROOT/config/routes.rb, add
post '/foo' => 'foo#index'
in the block
Step 6: start rails server
$ rails s
That's all. Now you can send POST requests to http://localhost:3000/foo and see what happens.

Non specific MVC framework ruby gems and example code to Rails framework

I have been learning Ruby on Rails, but I still have issues when it comes to Ruby gems with examples that are irb based and not Rails or Sinatra framework based. I am trying to implement the Block.io Bitcoin API functionality. But the code I find is Ruby only, so I am not sure where to create a config file for the API Key and also whether I need to create a controller to make this work in the views for Rails.
The gem and examples are on: https://github.com/BlockIo/gem-block-io
I installed this gem via bundle install on Rails
gem install block_io -v=1.0.6
The Ruby example show the following:
>> require 'block_io'
>> BlockIo.set_options :api_key=> 'API KEY', :pin => 'SECRET PIN', :version => 2
In Rails which config file would I enter the above api_key and pin?
In the example they show the code to get your address as follows:
BlockIo.get_my_address
Do I need to create a function in a controller such as:
def address
#my_address = BlockIo.get_my_addresses
end
and in the view use:
<%= #my_address %>
I need some guidance with regards to the above, any comment or assistance will be greatly appreciated.
require 'block_io' can go into Gemfile like gem 'block_io'. Rails/bundler will require it automaticaly for you as long as the gem name is also the file name you want to require from this gem.
BlockIo.set_options :api_key=> 'API KEY', :pin => 'SECRET PIN', :version => 2 can be put into an initilizer like config/initializers/block_io.rb. This way set_options is called only once when Rails starts a server or console or runner.
Put it like this into the file config/initializers/block_io.rb
BlockIo.set_options :api_key=> ENV['BLOCK_IO_API_KEY'], :pin => ENV['BLOCK_IO_PIN'], :version => 2
With the environment variables in use you don't commit any secret into your repo.
Now you should be able to call BlockIo.get_my_address within any action.

rspec-rails 2.14.2 with Rails 4.2 InvalidURIError

I have gem 'rspec-rails', '~> 2.14.2' in my Gemfile.
After upgrading from Rails 4.1 to Rails 4.2 I've got the following error while running rspec:
Failure/Error: get 'api/...'
URI::InvalidURIError:
the scheme http does not accept registry part: www.example.com:80api (or bad hostname?)
What's the fix?
Adding / to a path fixes the issue.
E.g.:
get 'api/...' => get '/api/...'
post 'api/...' => post '/api/...'
patch 'api/...' => patch '/api/...'
put 'api/...' => put '/api/...'
etc.
EDIT: the reason is explained here.

Devise with Rails 4

The team behind Devise announced via blogpost
http://blog.plataformatec.com.br/2013/05/devise-and-rails-4/ that it was releasing a version that is compatible with Rails 4, calling it '3.0 rc'. In the same blog post, it also said it's releasing Devise 2.2.4.
I'm trying to build a Rails 4 app. when I did gem install Devise, it installed 2.2.4, not the version compatible with Rails 4.
Fetching: devise-2.2.4.gem (100%)
Which I assume from the comments in the blogpost about strong parameters is not going to be compatible with Rails 4.
I looked at Devise's github page but it's not obvious to me how to install the version compatible with Rails 4. Can you assist?
https://github.com/plataformatec/devise
Note, I tried
gem install devise --version 3.0.0.rc1
but it said
ERROR: Could not find a valid gem 'devise' (= 3.0.0.rc1) in any repository
ERROR: Possible alternatives: devise
Devise is now compatible with Rails 4 out of the box as of the time of this answer.
Our end goal is to have users be able to register, log in and log out of the website. We'll also create a small partial view letting us know if we're logged in or out.
Install the Devise gem.
Open up your Gemfile and install the Devise gem.
gem 'devise'
Then in your terminal run the bundle install command to install the gem.
$ bundle install
Run some Devise generators to set up the initial configurations.
Run this command from your terminal:
rails generate devise:install
This generator installs the initializer that configures all of Devise's available settings.
Generate your User model.
Next we need to generate our User model. I'm going to name it User but you can name it whatever you like, just replace User with Whatever.
rails generate devise User
rake db:migrate
Configure your default URL option for Development.rb
Inside of config/environments/development.rb, set the Action Mailer's default URL to localhost:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Make sure you have a root route declared in Routes.rb
You need to make sure that routes.rb has a default root route - if you don't have one, set it!
root to: 'home#index'
Create a partial view to see if we're logged in or not.
Inside of your views/layouts folder create a file named _user_widget.html.erb and copy this code in:
<% if user_signed_in? %>
<p>Welcome <%= current_user.email %></p>
<%= link_to 'Logged In [click to logout]', destroy_user_session_path, :method => :delete %>
<% else %>
<p>You are not signed in.</p>
<%= link_to 'Login', new_user_session_path %>
<% end %>
And invoke it within your layout (views/layouts/application.html.erb):
<!DOCTYPE html>
<html>
<head>
<title>FacebookAuthTest</title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
<%= render 'layouts/user_widget' %>
</body>
</html>
Make sure you stop and restart the server otherwise you will find all sorts of nasty bugs! It's always best to restart your local
server when you update your gemfile or change anything in the
environment configuration file.
With all this in place, you should be able to sign up, log in and log out from your very own Rails website.
If you have any questions feel free to leave a comment below and I'll try to help.
UPDATE SEPT 17th, 2013: The master branch is now compatible with Rails 4. No need to search for another version.
Looking at the github repo, it looks like you want version 3.0.0.rc (no 1). So you'll want
gem install devise --version "3.0.0.rc"
or, in your gemfile:
gem 'devise', '3.0.0.rc'
This installed it
gem install devise --pre
or
devise-3.0.0.rc.gem
At this point this version of the gem is what you would want to use
gem 'devise', '3.0.0'
gem 'devise', github: 'plataformatec/devise', branch: 'rails4'
Now that the 3.0 version is stable, you can just do:
gem install devise
or in your Gemfile:
gem 'devise'

Trying to call methods from gems in a view, getting "no such file" error

I have just installed a base installation of rails and have edited the main page to view some basic html and a link to create a new blog post via ruby in the corresponding 'erb' file. I am trying to add some additional ruby commands on this same page via <%= %> tags.
<h1>Hello, Rails!</h1> <%= link_to "My Blog", posts_path %>
<p>
<%= require 'rubygems' %>
<%= require 'simplegeo' %>
<%= SimpleGeo::Client.set_credentials('token', 'secret') %>
<%= a = SimpleGeo::Client.get_context(coordinates,coordinates); a %>
</p>
When I load this page , I get the following error: no such file to load -- simplegeo
Can someone point me in the right direction? Many thanks!
In Rails 3, you need to add this gem to your "Gemfile" .. follow this: http://gembundler.com/rails3.html
Remove this completely.. Never do this in your views
<%= require 'rubygems' %>
<%= require 'simplegeo' %>
Once you restart your rails server, if you added "simplegeo" to your gemfile, it'll be auto-required.
Move this to your controller to start
SimpleGeo::Client.set_credentials('token', 'secret')
#simple_geo_client = SimpleGeo::Client.get_context(coordinates,coordinates)
Then in your view, you can access any variable that starts with #
To get started in Rails, check out http://railsforzombies.org/
In rails 3.x, you use Bundler to specify gems. No need to require, especially in views.
Correct way is:
Open Gemfile
Set/List required gems inside using format:
source :rubygems
gem "simplegeo"
gem "some_other_gem"
Run bundle install (or just bundle) command in console.
now restart your server and gems are auto required.
Check out Rails guide on how to start.
Most probably you did not install Simplegeo gem properly, or did not attach it correctly in you IDE.

Resources