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: { }'
Related
I have found many generic posts suggesting this has to do with redirects. I believe this may be due to how I have a form set up.
On the plans.html.erb page I have a form with four submits, each going to the same place with different params:
<%= form_with url: :affiliate_select_plan, class: "mx-auto" do |f| %>
<!-- Paid Plans -->
<% #plans.each_with_index do |plan, i| %>
<%= f.button 'Select Plan', value: plan[:name], type: 'submit' %>
<% end %>
<% end %>
I have the affiliate_select_plan_path setup in my routes.rb:
devise_scope :affiliate do
post 'affiliate/select_plan', :to => 'affiliates/registrations#select_plan'
end
The form successfully hits the select_plan method in the controller, which redirects it to the new_affiliate_registration_path, passing the needed params.
def select_plan
redirect_to new_affiliate_registration_path(plan: plan_params[:button])
end
The new method in the controller is called, directing the user to the sign up page:
# GET /resource/sign_up
def new
#plan = AffiliatePlan.find_by(nickname: params.permit(:plan)[:plan].downcase)
super
end
From this page, if the back button on the browser is selected, it will bring the user back to the page they were at before being at plans.html.erb.
Could this be related to the redirect_to?
EDIT:
Here are the logs:
Started GET "/" for 127.0.0.1 at 2020-02-25 19:06:02 -0500
Processing by Affiliates::RegistrationsController#plans as HTML
Rendering affiliates/registrations/plans.html.erb within layouts/application
Rendered affiliates/registrations/plans.html.erb within layouts/application (5.2ms)
Rendered layouts/_google_analytics.html.erb (0.5ms)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_header.html.erb (1.2ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 195ms (Views: 194.2ms | ActiveRecord: 0.0ms)
Started POST "/partner/select_plan" for 127.0.0.1 at 2020-02-25 19:06:13 -0500
Processing by Affiliates::RegistrationsController#select_plan as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ck8HGRryriXleQrUjCSKjTrIRLIw273EdSu4WZnFn3kAL1mMmk7jqR1tZgnPniHsMzHFMl81vPBRuvA0/W4uSw==", "button"=>"Local"}
Unpermitted parameters: :utf8, :authenticity_token
Redirected to http://localhost:3000/partners/sign_up?plan=Local
Completed 200 OK in 1ms (ActiveRecord: 0.0ms)
Started GET "/partners/sign_up?plan=Local" for 127.0.0.1 at 2020-02-25 19:06:13 -0500
Processing by Affiliates::RegistrationsController#new as HTML
Parameters: {"plan"=>"Local"}
AffiliatePlan Load (1.2ms) SELECT "affiliate_plans".* FROM "affiliate_plans" WHERE "affiliate_plans"."nickname" = $1 LIMIT $2 [["nickname", "local"], ["LIMIT", 1]]
↳ app/controllers/affiliates/registrations_controller.rb:11
Rendering affiliates/registrations/new.html.erb within layouts/application
Rendered affiliates/registrations/new.html.erb within layouts/application (4.6ms)
Rendered layouts/_google_analytics.html.erb (1.1ms)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_header.html.erb (1.2ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 191ms (Views: 187.6ms | ActiveRecord: 1.2ms)
I have a hunch that this might have to do with form resubmission: Forms and the back button tend to be a bit wonky at times.
However, instead of going more in depth with this, let me point you in another direction. I'm doing this because to me, this looks like a classic case of someone trying to find a solution to the wrong problem. I'm saying this because based on the code and log snippets you've provided, you're jumping through hoops to pass a parameter (in your case the name of a plan) via multiple actions – which, if I'm right, is just unnecessary.
Here's what I would do instead:
<% #plans.each do |plan| %>
<%=
link_to 'Select Plan',
new_affiliate_registration_path(plan: plan.downcase),
class: 'some-button-class
%>
<% end %>
This way, you don't have to mess around in your controllers in any way. Also, since there is no POST request, you won't have any issues with form (re)population and such things.
I am trying to do something really simple, render a json with some stuff.
I do have a view views/api/initialize.html.erb if this counts as useful info.
I have no idea what causes this but the variables filled alright, I checked
My controller:
class ApiController < ApplicationController
def initialize
#articles = Article.all
#areas = Area.all
#languages = Language.all
data_json = { articles: #articles, areas: #areas, languages: #languages }
render json: data_json
end
def index
end
end
My route:
get '/init', controller: 'api', action: 'initialize'
Here is my full error trace from development.log:
NoMethodError (undefined method `variant' for nil:NilClass):
app/controllers/api_controller.rb:9:in `initialize'
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.9ms)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (14.1ms)
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (44.1ms)
DEPRECATION WARNING: Accessing mime types via constants is deprecated. Please change `Mime::HTML` to `Mime[:html]`. (called from const_missing at /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/http/mime_type.rb:52)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.4ms)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.4ms)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.4ms)
DEPRECATION WARNING: Accessing mime types via constants is deprecated. Please change `Mime::WEB_CONSOLE_V2` to `Mime[:web_console_v2]`. (called from const_missing at /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/http/mime_type.rb:52)
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (36.5ms)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms)
Rendering /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms)
Rendered /usr/local/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (59.3ms)
Can you locate where the problem is here?
Thank you.
Initailize probably reserved already by Ruby. Just change something not reserved names like test_api_initialize or better api_init.
def api_inits
end
routes:
get '/init' => 'api#api_inits', as: :init
I have written a rails-api with two resources users and books. When I make a GET request for books it works properly, but for users it fails with the following message:
Started GET "/users/" for 127.0.0.1 at 2015-11-18 23:45:52 +0530
Processing by UsersController#index as HTML
User Load (0.5ms) SELECT `users`.* FROM `users`
Completed 500 Internal Server Error in 63ms (ActiveRecord: 4.2ms)
ArgumentError (wrong number of arguments (0 for 1)):
app/controllers/users_controller.rb:8:in `index'
Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.erb (17.6ms)
Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.1ms)
Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (69.4ms)
Here is the error I get on the browser:
ArgumentError in UsersController#index
wrong number of arguments (0 for 1)
Extracted source (around line #8):
...
# GET /users
def index
#users = User.all
render json: #users # this is line 8
end
...
config/routes.rb
Rails.application.routes.draw do
resources :users
resources :books
end
Any clue on what might be going wrong?
The error was with my model that was being passed to the serializer. I had a column in my database with the name method. method is a rails reserved word (read: ruby-and-rails-reserved-words) which was creating the problem. I renamed my column to registration_method which fixed the issue. Thanks everyone for the help :)
could try render #users.to_json
Is User.all returning any users?
Thank you for allowing me here. I have searched and searched for the answer to this riddle.
I am trying my first Rails app, and everything is straightforward: the index page summons a _calc.html.erb partial. And summons it, and summons it...
ETA FIXED! CODE BELOW -> the offending line is highlighted for those who encounter the error in the future:
<%= form_for :plan do %>
<em>OK, how much do you make?</em>
<%= number_field_tag :salary %>
<% puts "The salary field is producing the loop" %>
<h3><span id="more">Continue</span></h3>
<script type="text/javascript">
$("#more").click(function(){
$("div.subcalculator").animate({opacity: 1},800);
});
$("#moremore").click(function(){
**<% if check_expenses(:salary, :weeklyExpenses) %>**
$("div.sub_subcalculator").animate({opacity: 1},800);
**<% end %>**
});
</script>
<div id="subcalculator" class="subcalculator">
ETA ERROR: The screen that now comes up on localhost:3000 is
We're sorry, but something went wrong.
If you are the application owner check the logs for more information.
And in the Rails Server it says:
Started GET "/" for 127.0.0.1
at 2013-11-03 11:30:37 -0500 Processing by IntroController#index
as HTML The salary field is producing the loop The salary
field is producing the loop The salary field is producing the
loop The salary field is producing the loop The salary field
is producing the loopRendered plan/_calc.html.erb (1.3ms)
Rendered plan/_calc.html.erb (2.2ms) Rendered
plan/_calc.html.erb (3.1ms) Rendered plan/_calc.html.erb
(4.0ms) Rendered plan/_calc.html.erb (4.8ms) Rendered
plan/_calc.html.erb (5.6ms) Rendered plan/_calc.html.erb
(6.5ms) Rendered plan/_calc.html.erb (7.4ms) Rendered
plan/_calc.html.erb (8.2ms) Rendered plan/_calc.html.erb
(9.1ms)
I did ascertain the loop occurs in the top of the partial; the first helper is for a number_field_tag and that seems to render over and over but the subsequent number_field_tag(s) do not. I tend to compulsively rake the routes and migration if I've made even tiny changes, so if that helps at all:
root GET / intro#index
intro_index GET /intro/index(.:format) welcome#index
plan_index GET /plan(.:format) plan#index
POST /plan(.:format) plan#create
new_plan GET /plan/plan/calc(.:format) plan#new
edit_plan GET /plan/:id/edit(.:format) plan#edit
plan GET /plan/:id(.:format) plan#show
PATCH /plan/:id(.:format) plan#update
PUT /plan/:id(.:format) plan#update
DELETE /plan/:id(.:format) plan#destroy
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.