I am starting my learning process by first listing all the users.
First I had added an action in the controller as
def list_users
#users=User.find(:all)
end
And in the View users/list_users.html.erb I have added the line
list_users.html.erb:
<%= Time.now %>
<% #users.each do |user| %>
<%= user.firstname %>
<% end %>
And for routing I have added the routes as
map.list_users '/list_users', :controller => 'users', :action => 'list_users'
That's it .. when I run my app , it's showing me the error as
Development mode eh? Here is the error - #<ActionView::TemplateError:
ActionView::TemplateError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each) on line #7 of app/views/users/list_users.html.erb:
Why so?
Solution:
I myself find that the controller action is under protected, that's why it showed me the error.
It seems that you didn't use the login method in list_users.html.erb, where did you used it?
But here is another error (maybe typo error only?)
<% #users.each do |user| %>
<%= user.firstname # not #user here! %>
<% end %>
Related
Hi I am running into problems while trying to new record its showing an error message which error is not present in my code-
THe error message-
undefined method `user_cruds_path' for #<#<Class:0x00007f403e3162f0>:0x00007f403e3527a0>
Did you mean? user_crud_path
Extracted source (around line #8):
<h2>Create User</h2>
<%= form_for(#users) do |f| %>
<%= render(:partial => 'form', :locals => {:f => f}) %>
Rails.root: /home/ayan/Desktop/ror/q
Application Trace | Framework Trace | Full Trace
app/views/user_crud/new.html.erb:8:in `_app_views_user_crud_new_html_erb___1837862909000615994_69956949154020'
Request
Parameters:
None
Toggle session dump
_csrf_token: "DChk63/c8Jl1+JamtZiB03rFYMC/kybhyZr0/JixXDo="
flash: {"discard"=>[], "flashes"=>{"notice"=>"UserCrud updated successfully."}}
session_id: "aa128a12236d1d8d94061ed5de21d7f1"
Which is surprisingly some other request message , but the intended method call with url path helper "user_cruds_path" is not present-Its already corrected.
I have already restarted the server but same problem
Here is my controller class-
class UserCrudController < ApplicationController
def create
# Instantiate a new object using form parameters
#users = UserCrud.new(users_params)
##users=UserCrud.new(:name=>"Ayan",:email=>"ac#gmail.com")
# Save the object
if #users.save
# If save succeeds, redirect to the index action
flash[:notice] = "UserCrud created successfully."
redirect_to("/user_crud/index/")
else
# If save fails, redisplay the form so user can fix problems
render('new')
end
end
def new
#users = UserCrud.new({:name => 'Default',:email=>'deault#domain.ext'})
end
My view for new Record creation:(new.html.erb)
<% #page_title = "New User" %>
<%= link_to("<< Back to List", "/user_crud/", :class => 'back-link') %>
<div class="users new">
<h2>Create User</h2>
<%= form_for(#users) do |f| %>
<%= render(:partial => 'form', :locals => {:f => f}) %>
<div class="form-buttons">
<%= f.submit("Create User") %>
</div>
<% end %>
</div>
routees.rb
resources :user_crud do
member do
get :delete
end
end
Its aweird to see the error message referring to a path name that is not present at all-
Any help is very much appreciated.
The error is on the form, you are not specifying the correct path. When not setting this option, Rails will look for the "default path", which in this case is user_cruds_path
form_for(#users, url: "/user_crud/") works , but it would be great if anyone suggests what is the suitable url helper "/user_crud/"
Edit:
I got it from Marcelo's Comment and found the routes by command rails routes
And found the url helper to be user_crud_index_url for "/user_crud/"
So the final answer is:
form_for(#users,url: user_crud_index_url)
I have a seed database that renders Admin user information and links to individual member profile pages. The database renders images and data fine in the main page, but comes up nil <p>, with id </p> *== $0* when I try to call the individual objects on the show page.
routes.rb:
get '/team', to: 'pages#team'
get '/team/:id', to: 'pages#show', as: 'agent'
pages_controller.rb:
def team
#admins = Admin.where(role: :staff)
end
def show
#admin = Admin.find(params[:id])
end
views/pages/team.html.erb:
<%= link_to agent_path(agent.id), class:"team-link" do %>
.....
<% end %> (all working, routes stable)
views/pages/show.html.erb:
<p><% #admin.name %>, with id <% #admin.id %></p>
#rendering == $0
Where's the data connection breaking down? I've been working at this for a day or so now and this is a fairly solid wall for me.
Ah, your ERB statements are a bit off. You need to use the <%= %> format for the output of your ruby code to be shown (note the =).
So <% #admin.name %> should be <%= #admin.name %>, etc.
I have followed the Devise Wiki to create a very basic admin setup by adding a admin column to my User table in a boolean format.
I have been into my table (through SQlite administrator) and assigned one of my users to be an admin.
I am then have the following code in my view:
<% if user_signed_in? %>
<% if current_user.admin? %>
<%= link_to "Admin Job Post", new_user_job_path(current_user.id) %>
<% else %>
<%= link_to "Post a new job", new_user_job_path(current_user.id) %>
<% end %>
<% else %>
<%= link_to "Post a new job", new_user_session_path %>
<% end %>
The issue I am having is that my app is only ever returning my <%= link_to "Post a new job", new_user_job_path(current_user.id) %> even when logged in with an admin user.
It would be great to get a solution on this because I have tried several variations and can't get it to work.
Thanks in advance for your help!
I'm not sure of the entirety of your code, but I have a similar setup, but I used the following to show a link once a user is logged in as an Admin (I used 'try' due to the fact that it is outside of a 'user_logged_in' check).
<% if current_user.try(:admin?) %>
<li>AdminLink</li>
<% end %>
Are you sure that you made the user an admin?
To make the user admin you should have run the following commands
User.find(#id_of_user_you_want_to_make_admin)
User.admin = true
User.save!
<% if current_user.present? && current_user.has_role?(:admin) %>
<li><%= link_to 'Some Cool Admin Feature', cool_admin_path %></li>
This is what you're looking for if you're using Rails 4 with Devise 2.x. Note the () around :admin aren't necessary, I included them for clarity here as the '?' after the method seems to throw people that are new to ruby.
Another way to give admin privileges is to simply use <% if current_user.try (:email) == "admin#example.com" %> That always works for me. That way you don't have to worry about the whole system.
I have my application setup with a few different partials working well. I have asked here how to get a partial working to show the latest entry in the kase model, but now I need to show the latest 5 entries in the kase model in a partial.
I have duplicated the show most recent one partial and it's working where I need it to but only shows the last entry, what do I need to change to show the last 5?
_recent_kases.html.erb
<% if Kase.most_recentfive %>
<h4>The most recent case reference is <strong><%= Kase.most_recentfive.jobno %></strong></h4>
<% end %>
kase.rb
def self.most_recentfive
first(:order => 'id DESC')
end
Thanks,
Danny
EDIT
def self.most_recentfive
all(:order => 'id DESC', :limit=>5)
end
If I add the above code I get the following Error Message:
NoMethodError in Dashboard#index
Showing app/views/kases/_recent_kases.html.erb where line #2 raised:
undefined method `jobno' for #<Array:0x105984c60>
Extracted source (around line #2):
1: <% if Kase.most_recentfive %>
2: <h4>The most recent case reference is <strong><%= Kase.most_recentfive.jobno %></strong></h4>
3: <% end %>
def self.most_recentfive
all(:order => 'id DESC', :limit=>5)
end
EDITED TO ADD:
Then, in your partial, to display the results, you do
<% if Kase.most_recentfive %>
<h4>The most recent five case references are
<% Kase.most_recentfive.each do |k|%>
<strong><%= link_to k.jobno, k %></strong><br />
<% end %>
</h4>
<% end %>
I have a 1:1 has_one / belongs_to relationship between users and registrations. One user has one registration.
When I try to iterate through users in a view and display their registration info (source to follow), I get the following error:
ActionView::TemplateError: You have a nil object when you didn't expect it! The error occurred while evaluating nil.registration_code
Here's the offending view code:
<% #users.each do |user| %>
<%= user.registration.registration_code %>
<% end %>
In my users_controller.rb:
def users_registration_codes
#users = User.find(:all)
end
The likely issue here is that you're finding a particular User without an associated Registration - i.e. it's not that user == nil, but that user.registration == nil so it complains when you try to call registration_code() on the non-existent associated registration object
Try
<% #users.each do |user| %>
<%= user.registration.registration_code if user.registration %>
<% end %>