Rails 3: Create a new record by URL - ruby-on-rails

All:
What's the correct syntax for a new record to be created by passing all the parameters inside a URL, not from a Web form?
I'd like a different computer system to be able to concatenate a URL and create new records in my Rails app.
But, when I try to make a new record in "Scores", Rails won't add it.
http://myapp.heroku.com/scores?message_id=51&subscriber_id=167&amount=3&method=post
Where the desired attributes to add to a new scores record are message_id, subscriber_id, and amount.
Here's the Heroku log
2012-01-26T18:27:34+00:00 app[web.1]: Started GET "/scores?message_id=51&subscriber_id=167&amount=3&method=submit" for 122.17.234.171 at 2012-01-26 10:27:34 -0800
2012-01-26T18:27:34+00:00 app[web.1]: Processing by ScoresController#index as HTML
2012-01-26T18:27:34+00:00 app[web.1]: Parameters: {"message_id"=>"51", "subscriber_id"=>"167", "amount"=>"3", "method"=>"post"}
2012-01-26T18:27:34+00:00 app[web.1]: Rendered scores/index.html.erb within layouts/application (3.4ms)
2012-01-26T18:27:34+00:00 app[web.1]: Completed 200 OK in 16ms (Views: 4.2ms | ActiveRecord: 10.6ms)
2012-01-26T18:27:34+00:00 heroku[router]: GET postopmobile.heroku.com/scores dyno=web.1 queue=0 wait=0ms service=55ms status=200 bytes=7605
Here's my Scores model
class Score < ActiveRecord::Base
belongs_to :message
belongs_to :subscriber
end
Scores controller has all the standard Rails CRUD scaffolding, plus this (I'm running Devise):
before_filter :authenticate_user!, :except => [:new, :create]
Lastly, trying method=submit at the end of the above URL doesn't work either.
Any thoughts? I'm trying to figure out if it's just a syntax issue or a deeper Rails create issue.
Thanks.

New records by default in Ruby on Rails are created using POST. If you want to have them created using a GET request (which I would advise against) you need to create a custom route to your create action. For more info on the routes see: http://guides.rubyonrails.org/routing.html.

You need a POST request instead of GET. It will not work with passing 'method' at url.
If you really want to do that, you can add custom route, e.g.:
resources :scores do
get :make
end
Then add 'make' action at controller, which creates new record.
But it's extremely unsafely.

Related

Rails routes.rb assistance

thanks in advance for your help!!
I have in routes.rb:
get 'api/streets:name' => 'streets#get_by_name', as: "get_by_name"
I have in streets_controller.rb:
ids = params[:name]
I have in Javascript:
const params = encodeURI('name[]=1&name[]=2')
fetch(`/api/streets?${params}`)
When I call the api from the front end, I get the following log message:
Started GET "/api/streets?name%5B%5D=2&name%5B%5D=5" for 127.0.0.1 at 2019-09-06 17:10:59 -0700
Rendering pages/index.html.erb within layouts/application
Processing by PagesController#index as */*
Parameters: {"name"=>["2", "5"], "path"=>"api/streets"}
Rendered pages/index.html.erb within layouts/application (7.1ms)
Rendering pages/index.html.erb within layouts/application
Why is it using the PagesController and not the StreetsController?
In all other cases where I'm getting and posting and putting and deleting on the api, the router knows what controller to use. It's just this one case when I'm using array parameters where it's routing to the wrong controller.
It's probably just something dumb I've done.
Your route expects a path like: /api/streets123 - where params[:name] would be equal to "123" and no, that's not a typo.
You should just use:
get 'api/streets' => 'streets#get_by_name', as: "get_by_name"
If you need to enforce the existence of the :name parameter then you should use the :constraints option.

routes lead to blank view, no html

I have a very similar issue to this question: My route returns a blank view (no html when I do view source)
When I access a route, it just shows a blank view with no html when viewing source.
My route returns a blank view (no html when I do view source)
config/routes.rb
SampleApp::Application.routes.draw do
devise_for :users
root to: 'home#index'
end
app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
end
end
app/views/home/index.html.slim (slim is like erb, I tested both out but still no html rendered)
h1 get ready to party!
log/development.log
Started GET "/" for 127.0.0.1 at 2012-12-05 00:36:21 -0500
Processing by HomeController#index as */*
Rendered home/index.html.slim within layouts/application (0.4ms)
Completed 200 OK in 6ms (Views: 5.9ms | ActiveRecord: 0.0ms)
The other question suggested changing ports. I did all the basic stuff like restart server, restart computer, try different ports.
It sounds like your layout could be blank and is not rendering the index view.
The default layout is application.html.erb and should render your views with <%= yield %>.
Also - have you tried restarting your server for kicks? That's the answer to the other SO question you posted so I'm assuming you've tried that...

RESTful implementation of sitewide (multiple models) search with Sunspot Solr Search through a SearchController. Is that correct?

So I am using a SearchController to manage sitewide searches (user queries are full-text searched in two different models), and it works in the rails development server, but it doesn't work on Heroku. I suspect this means I am committing some sort of MVC or RESTful logic error because I think I have had this issue before with heroku and my localhost server where the latter was more lenient with my code. Should I be using a collection or member route of some sort?
Search Controller:
def index
#search = Post.search do
fulltext params[:search]
end
#posts = #search.results
#search = Group.search do
fulltext params[:search]
end
#groups = #search.results
end
Route:
resources :search, :only => [:index]
Search form (in HAML):
-form_tag '/search', :method => :get do
=text_field_tag :search, params[:search], :id => 'searchfield'
=submit_tag 'search',:name => nil, :id => 'searchbutton'
Heroku Logs from the failed action:
2011-12-16T04:05:20+00:00 app[web.1]: Started GET "/search?utf8=%E2%9C%93&search=integer" for
68.6.74.167 at 2011-12-15 20:05:20 -0800
2011-12-16T04:05:20+00:00 app[web.1]: Processing by SearchController#index as HTML
2011-12-16T04:05:20+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "search"=>"integer"}
2011-12-16T04:05:23+00:00 heroku[router]: GET stormy-robot-3215.heroku.com/search dyno=web.1 queue=0 wait=0ms service=3030ms status=500 bytes=728
2011-12-16T04:05:23+00:00 app[web.1]: Completed in 3022ms
2011-12-16T04:05:23+00:00 app[web.1]:
2011-12-16T04:05:23+00:00 app[web.1]: Errno::ECONNREFUSED (Connection refused - connect(2)):
2011-12-16T04:05:23+00:00 app[web.1]: app/controllers/search_controller.rb:3:in `index'
2011-12-16T04:05:23+00:00 app[web.1]:
2011-12-16T04:05:23+00:00 app[web.1]:
Thanks for your help. Let me know if I should give any more information
Your code is fine, the only problem is that your Heroku application can't connect to any Solr host.
If you're using the Websolr addon in Heroku then you should open a support ticket because this should be working. Otherwise, you should run this in the heroku console:
Sunspot.config.solr.url
If that points to "http://127.0.0.1:8983/solr" or similar then that's your problem. You need that to point to some internet-accessible Solr server.

Rails routes with / at the end

Is it possible to change the routes for one resource (at least only show, index) to have / at the end?
For example:
test.heroku.com/books/1/ #the last / will produce an error
Using Rails 3.0.9. Server heroku.com
routes.rb:
resources :books
Error:
2011-08-06T21:41:35+00:00 app[web.1]: Started POST "/books/1/?ref=bookmarks" for 213.229.110.100 at 2011-08-06 14:41:35 -0700
2011-08-06T21:41:35+00:00 app[web.1]:
2011-08-06T21:41:35+00:00 app[web.1]: ActionController::RoutingError (No route matches "/books/1"):
But when I point my browser to
http://test.heroku.com/books/1
is working
http://test.heroku.com/books/1/
is working too.
But if I use:
http://test.heroku.com/books/1/
as a canvas url for a Facebook Application is not working.
Thanks
EDIT Complete ERROR LOG:
2011-08-06T22:07:53+00:00 heroku[web.1]: State changed from starting to up
2011-08-06T22:08:13+00:00 app[web.1]:
2011-08-06T22:08:13+00:00 app[web.1]:
2011-08-06T22:08:13+00:00 app[web.1]: Started POST "/books/1/?ref=bookmarks" for 213.229.110.100 at 2011-08-06 15:08:13 -0700
2011-08-06T22:08:13+00:00 app[web.1]:
2011-08-06T22:08:13+00:00 app[web.1]: ActionController::RoutingError (No route matches "/books/1"):
2011-08-06T22:08:13+00:00 app[web.1]:
2011-08-06T22:08:13+00:00 app[web.1]:
2011-08-06T22:08:13+00:00 app[web.1]:
2011-08-06T22:08:13+00:00 heroku[router]: POST test.heroku.com/books/1/ dyno=web.1 queue=0 wait=0ms service=136ms status=404 bytes=728
2011-08-06T22:08:13+00:00 heroku[nginx]: POST /books/1/?ref=bookmarks HTTP/1.1 | 213.229.110.100 | 904 | http | 404
That wont produce an error if you are making use of the built-in resourceful routing:
resources :books
If you have defined a custom route (perhaps in the process of upgrading a 2.3 app to Rails 3) without accounting for a trailing slash, then you might not be so lucky. If this is the case and you're not sure how to adjust, you should edit your question and add the relevant part of your routing file (config/routes.rb).
Update
Okay so after looking at the log output you posted I see what's happening. Facebook is requesting /books/1/?ref=bookmarks. This is NOT the same as /books/1/, which Rails interprets as actually being a request to the /books/1 resource. Why bother with the trailing slash in this case? It's inaccurate canonically speaking, and clearly it's causing problems.
Update 2
You can add POST /books/1 as a route to the resourceful routes like so:
resources :books do
post '/' => :show, :on => :member
end

Routing Discrepancy Between Development and Production Environments (Ruby on Rails)

I'm writing an app in Rails (v3.0.5), which I'm deploying to Heroku.
When I visit http://localhost:3000/places/new in my development environment, I'm taken to the appropriate page (the form for creating a new place). Everything works as expected (I can create new places).
When I try to visit the corresponding page (http://example.heroku.com/places/new) in my Heroku production environment, I'm routed back to the home page for my app.
The contents of my routes.db file:
ExampleSite::Application.routes.draw do
resources :users
resources :sessions, :only => [:new, :create, :destroy]
resources :places
root :to => 'pages#home'
match '/contact', :to -> 'pages#contact'
match '/about', :to -> 'pages#about'
match '/signin', :to -> 'sessions#new'
match '/signout', :to -> 'sessions#destroy'
end
What might be a cause for the discrepancy between development and production?
Note: the only actions I've built out so far in my 'places' controller are 'new' and 'create' (both of which perform as expected in the development environment). Not sure that should be relevant, but keep it in mind. Also, all of the 'users' actions and routes seem to be working as expected in both development and production.
EDIT: As noted in the comment below, /places/new is an authentication-protected page, but in both cases I'm trying this while logged in. Also, when I do try to access /places/new in my production environment while not logged in, the appropriate redirect (to my /signin page) works as expected.
My Heroku log from an attempt at getting /place/new:
2011-05-12T23:37:30+00:00 app[web.1]: Started GET "/places/new" for 74.87.126.82 at Thu May 12 16:37:30 -0700 2011
2011-05-12T23:37:30+00:00 app[web.1]: Processing by PlacesController#new as HTML
2011-05-12T23:37:30+00:00 app[web.1]: Redirected to http://example.heroku.com/
2011-05-12T23:37:30+00:00 app[web.1]: Completed 302 Found in 4ms
2011-05-12T23:37:30+00:00 heroku[router]: GET example.heroku.com/places/new dyno=web.1 queue=0 wait=0ms service=9ms bytes=631
2011-05-12T23:37:30+00:00 app[web.1]:
2011-05-12T23:37:30+00:00 app[web.1]:
2011-05-12T23:37:30+00:00 app[web.1]: Started GET "/" for 74.87.126.82 at Thu May 12 16:37:30 -0700 2011
2011-05-12T23:37:30+00:00 app[web.1]: Processing by PagesController#home as HTML
2011-05-12T23:37:30+00:00 app[web.1]: Rendered layouts/_header.html.erb (4.3ms)
2011-05-12T23:37:30+00:00 app[web.1]: Rendered pages/home.html.erb within layouts/application (5.7ms)
2011-05-12T23:37:30+00:00 app[web.1]: Completed 200 OK in 7ms (Views: 3.5ms | ActiveRecord: 5.8ms)
2011-05-12T23:37:30+00:00 heroku[router]: GET example.heroku.com/ dyno=web.1 queue=0 wait=0ms service=14ms bytes=2357
As it turns out, I had a second (forgotten, since I changed the scope of 'users') authentication condition which checked for admin-level status. THAT condition redirected to the home page if the current user was not an admin.
The test user I created in my development environment IS an admin (since I created that user before switching the scope), while the test user I created in the production environment IS NOT an admin (hence the redirect).
Thanks all for your help! While this mistake was an oversight, your suggestions taught me about a few new diagnostic tools I hadn't thought to use.

Resources