Simple-navigation: undefined local variable or method `root' - ruby-on-rails-4

I installed and configured simple-navigation as decribed here (simple-navigation configuration).
I modified config/navigation.rb (contains only one menu item):
primary.item :key_1, 'Home', root
My config/routes.rb contains:
root 'static_pages#home'
rake routes contains string:
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
app/views/layouts/application.html.erb contains:
<%= render "layouts/header" %>
app/views/layouts/_header.html.erb contains:
<%= render "layouts/main_nav" %>
and app/views/layouts/_main_nav.html.erb contains one string:
<%= render_navigation %>
After that I got an error:
Showing <...>_main_nav.html.erb where line #1 raised:
undefined local variable or method `root'
Why restful route helpers and named route helpers are "invisible" in this case? I tried all of the prefixes from the routes table.

The 3rd argument should be an URL or path, so change root to root_path in config/navigation.rb. Your menu item should look like:
primary.item :key_1, 'Home', root_path

Related

In Rails, how do I define a "_new_path"?

I’m using Rails 4. How do I define a “_new_path” in my routes.rb file? I’m tryhin got create an admin area where an admin can create objects. So I have this in my config/routes.rb file
get 'admin/index'
get 'admin/list'
get 'admin/add'
get 'admin/approve'
Then in my app/views/admin/_add.html.erb file, I have
<%= form_for #my_object do |f| %>
but when I visit my page, I get the error
undefined method `my_objects_path' for #<#<Class:0x007ffea5de2c80>:0x007ffea5dd9798>
Did you mean? my_objects_new_path
I want the form to submit to the “create” method in my controller. How do I set this up?
try this commande rake routes to show yours paths rake routes
admin_index GET /admin/index(.:format) admin#index
admin_list GET /admin/list(.:format) admin#list
admin_add GET /admin/add(.:format) admin#add
admin_approve GET /admin/approve(.:format) admin#approve
after in app/views/admin/_add.html.erb file
<%= form_for admin_add_path do |f| %>

Rails controller specific assets for selected controllers only

I want to use the controller specific assets only for some controllers, but not for all. Hence, I didn't add the assets for which I don't need separate files. But this leads to 404 for those resources as they are not present, e.g. my app/views/layouts/dashboard.html.erb file contains the following piece of code to generate the controller specific js/css:
<%= stylesheet_link_tag 'dashboard', params[:controller], media: 'all' %>
<%= javascript_include_tag 'dashboard', params[:controller] %>
However, I do not want to create separate files for Admin controller, as the admins page has no new css/js required. But, app tries to locate the assets for the controller and since not found it returns the 404 as follows:
GET http://dashboard.localhost.com:3000/stylesheets/dashboard/admins.css 404 (Not Found)
GET http://dashboard.localhost.com:3000/javascripts/dashboard/admins.js 404 (Not Found)
What should be done to prevent this?
Moreover, there are a few controllers for which the assets are obviously not required, e.g. Registrations controller, leading to 404 for http://0.0.0.0:3000/stylesheets/store/registrations.css for http://0.0.0.0:3000/account/edit page
Here is what I did to fix this in my application.html.erb:
<%= stylesheet_link_tag 'dashboard' %>
<%= stylesheet_link_tag params[:controller] if InmonarchWebsite::Application.assets.find_asset("#{params[:controller]}.css") %>
<%= javascript_include_tag 'dashboard' %>
<%= javascript_include_tag params[:controller] if InmonarchWebsite::Application.assets.find_asset("#{params[:controller]}.js") %>

phoenix: render template of other folder

I have two template folder in my web/templates folder:
> ls web/templates
personal_info user
What I want is to render some template from user folder in another view of personal_info. so I have a file at path: web/templates/personal_info/index.html.eex, I have following content:
<%= render "user/xyz.html" %>
But I get following error:
[error] #PID<0.821.0> running MyApp.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /
** (exit) an exception was raised:
** (Phoenix.Template.UndefinedError) Could not render "user/xyz.html" for MyApp.PersonalInfoView, please define a matching clause for render/1 or define a template at "web/templates/personal_info". The following templates were compiled:
* index.html
Please tell me how do I render template defined in other folder, I tried few permutations, but none worked.
Phoenix templates are just functions, so when you want to render your UserView's "xyz.html" template from your PersonalInfo's view, you just call the function!
Let's say you are inside the web/templates/personal_info/show.html.eex template. (Phoenix.View.render is already imported for you):
<%= render UserView, "xyz.html", user: user %>
Of if you want to pass along all the template assigns that your PersonalInfo template was provided:
<%= render UserView, "xyz.html", assigns %>
As you found, this works from anywhere, because templates are Just Functions. For example, the same thing would work in iex:
iex> Phoenix.View.render(MyApp.UserView, "xyz.html")
"<h1>User ..."
As of Phoenix 1.5.1
Phoenix matches controllers, views, and templates by names. It can be changed by put_view like:
conn
|> put_view(MyAppWeb.SpecialView)
|> render(:show, message: "Hello")
https://hexdocs.pm/phoenix/Phoenix.Controller.html#render/3-views
For me worked when I specified application name:
web/templates/product_gallery/index.html.eex:
<p>Please, render me!</p>
web/templates/kitchen/index.html.eex:
<%= render APP.ProductGalleryView, "index.html", assigns %>
If I am trying to render without application name I am getting:
undefined function ProductGalleryView.render/2 (module ProductGalleryView is not available)
I'm on Phoenix 1.3.0. Seems like I had to add
alias MyApp.Userview
to web/views/personal_info_view.ex, then
<%= render conn, UserView, "xyz.html" %>
Without the alias above, then you have to
<%= render conn, MyApp.UserView, "xyz.html" %>
Apparently following worked:
<%= Phoenix.View.render(MyApp.UserView, "xyz.html") %>
please let me know, If there are better alternatives.
Source: this.

rails4, NameError in Zic#index, routes

undefined local variable or method `nov_tisk_revij_index' for #<#<Class:0x007f361551b858>:0x007f3615519c60>
Hey guys, I'm setiing up a rails app and can't figure out what's wrong. I used rails g controller nov_tisk_revij index show to create my views and controller and now I can't link to it. If you can spot what I'm doing wrong, I'd be very grateful, don't want to remake this as is took a fair amount of work.
rake routes returns:
Prefix Verb URI Pattern Controller#Action
nov_tisk_revij_index GET /nov_tisk_revij/index(.:format) nov_tisk_revij#index
nov_tisk_revij_show GET /nov_tisk_revij/show(.:format) nov_tisk_revij#show
zic_index GET /zic/index(.:format) zic#index
root GET / zic#index
my html:
<%= link_to 'Novi izvodi tiskanih revij', nov_tisk_revij_index %>
routes:
get 'nov_tisk_revij/index'
get 'nov_tisk_revij/show'
get 'zic/index'
I tried adding resources :nov_tisk_revij into routes, and it still didn't work, just created additional routes for views I do not have.
controller:
class NovTiskRevijController < ApplicationController
def index
end
def show
end
end
I'm using rails4 with ruby 2.
<%= link_to 'Novi izvodi tiskanih revij', nov_tisk_revij_index_path %>
works

How to render a view inside of another view (rails 4)

Hi I want to preface this by saying that I am new to coding.
I have an issue that I believe can be solved in two ways
A. by rendering a partial
B. by updating the controller
( I can totally be wrong but these are what I suspect lol)
I have two controllers/views "reviews" and "logs". and I want them to both appear on the same page.
How can I do this?
I tried rendering a partial but I alway get an error.
and I tried the piece of code below:
which made my reviews show up on the page, but when I add
#log = #user.logs.all
to it, it doesn't pull all the logs to the page like it does for the reviews.
def show
#user = User.find_by_name(params[:id])
if #user
#reviews = #user.reviews.all
render action: :show
else
render file: 'public/404', status: 404, formats: [html]
end
end
First things first. Views refer to actions in controllers. So there can be several views for each controller or even none.
So, if you want to render #reviews and #logs on the same page you should first instantiate both instance variables in the same action and then render both partials in the same action.
How do you do that? Easy.
First you got to the controller you just showed and edit that show action.
def show
# You can set the variable in the if-clause
# And you also need to use static finders with a hash as an argument in Rails4
if (#user = User.find_by(name: params[:id]))
#reviews = #user.reviews.all
#logs = #user.logs.all
# You don't need to call render explicitly
# if you render the view with the same name as the action
else
render file: 'public/404', status: 404, formats: [html]
end
end
Second: you go to your /app/views/reviews/show.html.erb template and put both partials there like this (this is just an example, adjust your markup to fit your needs).
<h1> Reviews and Logs</h1>
<div id="reviews_part">
<%= render #reviews %>
</div>
<div id="logs_part">
<%= render #logs %>
</div>
Now create 2 new partials /app/views/reviews/_review.html.erb and /app/views/logs/_log.html.erb and put all the needed markup there (use regular variables review and log to adress the repeating objects). Rails will automaticaly repeat those partials as many times as needed.
Or you can explicitely call the partial render
<div id="reviews_part">
<% #reviews.each do |review| %>
<%= render review %>
which is the same as
<%= render partial:"reviews/review", locals:{review:review} %>
<% end %>
</div>
Here is the way of rendering partials into views in HAML:
=render :partial => "header"
%h2 Hello World
=render :partial => "footer"
Every partial you render this way, has to be created within the same folder.
Each partial's name has to begin with an underscore (_). This should be the view's directory:
- home
- index.html.haml
- _header.html.haml
- _footer.html.haml