apn_on_rails breaking view helpers on rails 2.3.8? - ruby-on-rails

I am developing a rails application which should send push notifications to iOS devices. I am using apn_on_rails gem. Everything works fine in the rails console. However when I run the server, I get undefined method errors for all the basic view methods such as content_for or form_tag etc:
When I take require 'apn_on_rails' from my development.rb out, the views work again, but of course without push notifications.
Here is an example trace:
Processing DashboardController#index (for 127.0.0.1 at 2011-02-27 13:55:59) [GET]
User Load (0.2ms) SELECT * FROM "users" WHERE ("users"."id" = 1)
CACHE (0.0ms) SELECT * FROM "users" WHERE ("users"."id" = 1)
Rendering template within layouts/dashboard
Rendering dashboard/index
ActionView::TemplateError (undefined method `content_for' for #<ActionView::Base:0x103343970>) on line #1 of app/views/dashboard/index.html.erb:
1: <% content_for :header do %>
2: <%= render :partial => "header", :locals => {:title => "Dashboard"} %>
3: <% end %>
app/views/dashboard/index.html.erb:1
app/controllers/dashboard_controller.rb:6:in `index'
Rendered rescues/_trace (28.5ms)
Processing ApplicationController#index (for 127.0.0.1 at 2011-02-27 13:55:59) [GET]
ActionView::TemplateError (undefined method `debug' for #<ActionView::Base:0x103343970>) in /Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/templates/rescues/_request_and_response.erb:
Rendered rescues/_trace (24.0ms)
/!\ FAILSAFE /!\ Sun Feb 27 13:55:59 +0100 2011
Status: 500 Internal Server Error
ActionView::TemplateError (undefined method `debug' for #<ActionView::Base:0x1031eaad8>) in /Library/Ruby/Gems/1.8/gems/actionpack-2.3.8/lib/action_controller/templates/rescues/_request_and_response.erb:
In /library/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/templates/rescues/_request_and_response.erb
Thanks for any help...

It is not evident whether you call config.gem 'apn_on_rails' - it sounds more like you are require:ing the library, which probably is something you should not do at this point: the point of environment setup.
Have the config.gem things in your environment setup (i.e. development.rb), but make eventual requires happen later (->in the controller / model / helper in question).

Related

react-rails with jbuilder doesn't work

I try to use react-rails with jbuilder.
For example, I use this great sample app of react-rails and try to rewrite to use jbuilder as a json response using reference from this part in react-rails.
These are the main rewrite parts:
# app/views/comments/index.html.erb
<%= react_component 'CommentBox', render(template: 'comments/index.json.jbuilder'), {prerender: true} %>
# app/views/comments/index.json.jbuilder
json.presenter do
json.comments(#comments) do |comment|
json.extract! comment, :id, :author
end
json.form do
json.action comments_path
json.csrf_param request_forgery_protection_token
json.csrf_token form_authenticity_token
end
end
json.imgSrc image_path("gundam.jpg")
But I got an following error:
Started GET "/comments" for 127.0.0.1 at 2015-04-07 18:26:40 +0900
Processing by CommentsController#index as HTML
Comment Load (0.9ms) SELECT "comments".* FROM "comments" ORDER BY "comments"."id" DESC LIMIT 5
Rendered comments/index.json.jbuilder (8.4ms)
Rendered comments/index.html.erb within layouts/application (34.6ms)
Completed 500 Internal Server Error in 43ms
ActionView::Template::Error (SyntaxError: Unexpected token o):
app/views/comments/index.html.erb:3:in `_app_views_comments_index_html_erb___3472795088323540071_70123597415980'
Do you have any idea to solve this error?
Thanks in advance.
see full source code:
Use jbuilder but it gets an error · jwako/sample-react-rails-app#508d581
It looks like the unexpected token is raised because the CommentBox component is calling JSON.parse() in getInitialState when this.props.presenter is already a JSON object.
You can fix it by removing JSON.parse() in getInitialState and returning only this.props.presenter.
Here is a related answer on parsing existing JSON objects.

Select_Tag not working in rails?

Here is my code for select_tag
<%= select_tag :tp, options_for_select([["movable",1],["fixed",2]]), :prompt => "select type of asset" %>
it works if i replace by:
<%= f.text_area :tp %>
If this in not enough i will give more code as you desire:
here is the log after i use select_tag
Started POST "/assets" for 127.0.0.1 at 2014-03-07 23:40:34 +0530
Processing by AssetsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Gib6rSODjXgXLZ9+5sDcq0ZatkA7144hU+Em2X7KONU=", "asset"=>{"name"=>"", "location"=>"", "cost"=>""}, "tp"=>"1","commit"=>"Save"}
(1.0ms) BEGIN
(1.0ms) ROLLBACK
Redirected to http://localhost:3000new
Completed 302 Found in 5ms (ActiveRecord: 2.0ms)
[2014-03-07 23:40:34] ERROR URI::InvalidURIError: the scheme http does not accept registry part: localhost:3000new (or bad hostname?)
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/uri/generic.rb:1203:in `rescue in merge'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/uri/generic.rb:1200:in `merge'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpresponse.rb:275:in `setup_header'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpresponse.rb:205:in `send_response'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:110:in'run'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
Started GET "/assets/logo.png" for 127.0.0.1 at 2014-03-07 23:40:36 +0530
Error after changing to `real_assets_path
Started GET "/real_assets/new" for 127.0.0.1 at 2014-03-09 13:56:47 +0530
ActiveRecord::SchemaMigrationLoad(1.0ms)SELECT"schema_migrations".* FROM"schema_migrations"
Processing by AssetsController#new as HTML
Rendered assets/new.html.erb within layouts/application (30.0ms)
Completed 500 Internal Server Error in 98ms
ActionView::Template::Error (undefined method `assets_path' for #<# <Class:0x545bb78>:0x545aeb8>):
1: <div class = "container">
2: <div id = "assetnew">
3: <%= form_for(#asset) do |f| %>
4: <center><h1>Add Asset</h1></center>
5: <div class = "well">
6: <a class ="red">
app/views/assets/new.html.erb:3:in`_app_views_assets_new_html_erb___457532114_11608392'
Rendered
F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered
F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered
F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (18.0ms)
Assets
This is the error Rails is returning:
ERROR URI::InvalidURIError: the scheme http does not accept registry part: localhost:3000new (or bad hostname?)
Without seeing your form, it seems you've got an issue with calling your resource /assets (as mentioned in the comments)
Basically, Rails treats the assets pipeline as a base directory - localhost:3000/your_asset_file.png -- it looks like this is what's happening here
Because your app is referencing /assets, I would surmise it's confusing it with the assets dir. Try doing this:
#config/routes.rb
resources :assets, as: "real_assets", path: "real_assets" #-> real_assets_path - /real_assets
Select
The select problem is likely caused by your assets path problem
select_tag is a standalone helper method (doesn't need any FormBuilder to help it). This means you should be able to just use it in an .html.erb file without any problems
The syntax for the select looks okay, so I'd recommend if you get your assets path fixed, you should be able to use it

Undefined method `each' for nil:NilClass in a partial

I am trying to display a collection as a partial ( on a side panel). I get an error above, have been fighting for several hours and do not understand what is the problem.
Containing view (a partial template included into application.html.haml file):
%span.span12
%ul.nav.nav-pills.nav-stacked{style: "color: white;"}
= render :partial => 'categories/categories_list'
Partial /categories/_category_list.html.haml:
%ul
- #categories.each do |category|
%li{style: "color: white;"}=category.name
Controller (categories_controller):
class Macro::CategoriesController < ApplicationController
def categories_list
#categories= Category.all
end
end
I have separately tried to add "collection: #categories" as an option for render :partial - to non avail. When I replaced the instance variable with just an array in order to test the partial rendering, it worked ( the list items got rendered). Seem like the variable #categories does not get instantiated, however I have double-checked in console that a given activerecord query returns a set of objects. I am breaking my brain to see where I made a mistake.
Log:
Started GET "/ios/macro/namespace2/another" for 127.0.0.1 at 2014-02-11 11:41:23 +0100
Processing by Macro::Namespace2::AnotherController#index as HTML
Parameters: {"param1"=>"xx"}
....... (SQL queries for that view)
Rendered macro/namespace2/another_controller/_another_view.html.haml (7.0ms)
Rendered macro/namespace2/another_controller/index.html.haml within layouts/application (29.6ms)
Rendered layouts/_logout_link.html.haml (0.5ms)
Rendered layouts/_top_navigation.html.haml (3.3ms)
Rendered layouts/_search.html.haml (0.8ms)
Rendered macro/categories/_categories_list.html.haml (0.8ms)
Rendered macro/_navigation.html.haml (3.6ms)
Completed 500 Internal Server Error in 49ms
ActionView::Template::Error (undefined method `each' for nil:NilClass):
1: %ul
2: - #categories.each do |category|
3: %li{style: "color: white;"}=category
app/views/macro/categories/_categories_list.html.haml:2:in `_app_views_macro_categories__categories_list_html_haml__246519878332953677_70248836672780'
app/views/macro/_navigation.html.haml:11:in `_app_views_macro__navigation_html_haml__3071184578230019020_70248793690940'
app/views/layouts/application.html.haml:24:in `_app_views_layouts_application_html_haml__3208937943371424843_70248793607300'
Rendered /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Rendered /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (10.6ms)
Routes:
namespace "macro" do
namespace "namespace2" do
resources :another, only: [:index]
end
resources :categories
end
end
Probably the #categories_list action in Macro::CategoriesController is not being called. Check the log and see which controller and action is handling the request.
You should see something like:
'Processing by Macro::CategoriesController#categories_list as HTML'
'Parameters: { }'

RoR - Devise Confirmable email error

I'm using Ruby on Rails 3 with Devise and sending confirmation emails. The emails work correctly (as far as sending) but when I click on the Confirm my account link, it generates an error.
Can anyone shed some light, or at least point me in the right direction on what this is/how to fix this?
This is the error I see in the logs:
Started GET "/users/confirmation?confirmation_token=qKvZWHgj6ncYAyuQq3e1" for 127.0.0.1 at 2012-08-15 20:57:11 +0300
Processing by ConfirmationsController#show as HTML
Parameters: {"confirmation_token"=>"qKvZWHgj6ncYAyuQq3e1"}
User Load (9.0ms) SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = 'qKvZWHgj6ncYAyuQq3e1' LIMIT 1
Completed 500 Internal Server Error in 35920ms
NoMethodError (undefined method `only_if_unconfirmed' for #):
app/controllers/confirmations_controller.rb:58:in `with_unconfirmed_confirmable'
app/controllers/confirmations_controller.rb:40:in `show'
Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb (8.0ms)
Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (3.0ms)
Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (4479.3ms)
I verified that the token in the url is an exact match for this user in the db.
Probably you have a really recent version of devise, and it changed the way the confirmation token is handled, so if you generate the views with devise like so:
rails g devise:views
Then under the app/views/devise/mailer/confirmation_instructions.html.erb file you have something like:
....
<%= link_to 'Confirm my account', confirmation_url(#resource, :confirmation_token => #resource.confirmation_token) %>
....
You should change that for:
<%= link_to 'Confirm my account', confirmation_url(#resource, :confirmation_token => #token) %>
As you can see they use an instance variable called #token instead of #resource.confirmation_token
That should do the trick!

Rails 3 - Passing models to another controller & losing stylesheets on render

I am attempting to build a simple content management system using Twitter's Bootstrap for a small site and I'm running into an issue with the views.
Rails version is 3.0.10
I want to allow the user to create their own pages so I have a Pages controller that has all your standard RESTful methods. Since these can have crud applied to them, they can only be accessed by a logged-in administrator.
So... I have a Public controller that sets the #pages and #page instance variables and uses those to display them in the public Show view.
Here is the Public controller - pretty simple:
def index
#pages = Page.all
end
def show
#pages = Page.all
#page = Page.find(params[:id])
end
The reason that there is a #pages instance variable on the 'index' method is because I have a welcome page that loads and I'm passing in #pages to populate the navigation elements dynamically:
<div class="nav-collapse">
<ul class="nav">
<li><%= link_to "Welcome", public_index_path, :class => 'active', :id => 'menu_home' %></li>
<% #pages.each do |page| %>
<li><%= link_to page.title, public_path(page) %></li>
<% end %>
</ul>
</div><!--/.nav-collapse -->
The content of the Public controller's 'show' method (which contains an individual page) is passed into the <%= yield %> statement in the applicaton.html.erb file:
<div class="container">
<%= yield %>
<hr>
<footer>
<p>My site</p>
</footer>
</div> <!-- /container -->
So far, this is working fine.
The problem is that when I click the link that takes me to the public_path(page) - I get the content as it should appear, but I lose all my styles. I'm actually getting a 404 error on my stylesheets:
All of these style sheets were loading in just fine on the http://localhost:3000/public page, but when it goes to http://localhost:3000/public/1 - that's when all the styles disappear. But, they are both using the same layout.
Here is what the log file shows when the request is made:
Started GET "/public/1" for 127.0.0.1 at 2012-03-03 21:14:49 -0600
Processing by PublicController#show as HTML
Parameters: {"id"=>"1"}
[1m[35mPage Load (1.0ms)[0m SELECT "pages".* FROM "pages"
[1m[36mPage Load (0.0ms)[0m [1mSELECT "pages".* FROM "pages" WHERE "pages"."id" = 1 LIMIT 1[0m
Rendered public/show.html.erb within layouts/application (24.0ms)
Completed 200 OK in 63ms (Views: 51.0ms | ActiveRecord: 1.0ms)
Started GET "/public/stylesheets/bootstrap.css" for 127.0.0.1 at 2012-03-03 21:14:50 -0600
ActionController::RoutingError (No route matches "/public/stylesheets/bootstrap.css"):
Rendered c:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.0.10/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (2.0ms)
Started GET "/public/stylesheets/bootstrap-responsive.css" for 127.0.0.1 at 2012-03-03 21:14:50 -0600
ActionController::RoutingError (No route matches "/public/stylesheets/bootstrap-responsive.css"):
Rendered c:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.0.10/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.0ms)
Started GET "/public/stylesheets/elements.less" for 127.0.0.1 at 2012-03-03 21:14:51 -0600
ActionController::RoutingError (No route matches "/public/stylesheets/elements.less"):
Rendered c:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.0.10/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (3.0ms)
Thanks for having a look
I managed to find my own answer...
The Rails helper stylesheet_link_tag was generating a relation attribute of type "stylesheet" when the Less css files needed a relation of type "stylesheet/less".
The fix was specifying the relation like this:
<%= stylesheet_link_tag 'elements.less', :rel => 'stylesheet/less' %>
<%= stylesheet_link_tag 'main.less', :rel => 'stylesheet/less' %>
Try to change path to your resource, something like that:
# config/routes.rb
resources :public, :path => :my_public
This will give you routes
public_index GET /my_public(.:format) public#index
POST /my_public(.:format) public#create
new_public GET /my_public/new(.:format) public#new
edit_public GET /my_public/:id/edit(.:format) public#edit
public GET /my_public/:id(.:format) public#show
PUT /my_public/:id(.:format) public#update
DELETE /my_public/:id(.:format) public#destroy
This way it will not mess up with requests to the static assets in public directory. But you may want to rename resource and controller instead.
If you are using the bootstrap-saas gem, make sure you take out the "link href='assets/css/bootstrap-responsive.css' rel='stylesheet'" tag out of your application.html.erb file, this will prevent the helper from generating the relation which results in a routing error.

Resources