Rails - updating a User leads to an error - ruby-on-rails

So I want to update an user in User table on the page and then when it's done, redirect to that user's page.
Whenever I do this, I get this error:
undefined method `name' for nil:NilClass
<% provide(:title, #user.name) %>
<p>
<%= #user.name %>
</p>
This is how my update looks like:
def update
#user = User.find(params[:id])
if #user.update_attributes(user_params)
flash[:success] = "Update successful"
redirect_to #user
else
render 'edit'
end
end
The page I redirect to:
<% provide(:title, #user.name) %>
<p>
<%= #user.name %>
</p>
What am I doing wrong here? Will provide more info if needed.
Edit:
Show action:
def show
if signed_in?
#user = User.find(params[:id])
end
end

Related

Flash messages only show after redirecting twice

flash[:errors] does not show after redirect_to in create method. But if I send another invalid form it shows up on the second and all following redirects. The same goes for flash[:success] when there are no errors.
I've tried using flash.keep in both this and the route I am redirecting to, and the views work since the messages do appear after multiple redirects. I'm wondering if it is because the redirects are to routes that render too?
In Controller:
def create
user = User.create(user_params)
if user.errors.any?
flash[:errors] = user.errors.full_messages
redirect_back(fallback_location: root_path)
else
flash[:success] = "USER SUCCESSFULLY CREATED"
redirect_to root_path
end
end
In Views:
<% if flash[:errors] %>
<% flash[:errors].each do |error| %>
<p style="color:red;"><%= error %></p>
<% end %>
<% end %>
AND:
<% if flash[:success] %>
<p style="color: red;"><%= flash[:success] %></p>
<% end %>
No flash messages are shown after the first redirect. After or more redirects, the flash messages are shown

No template found for UsersController#create, rendering head :no_content

OK, previously I had a problem with a no template error from users#create, now it complete 200 OK however does not redirect at all. Below is my edited users_controller.rb
I have a Signup, Login, Logout rails application with users as a resource. I am trying to save the first user in the database so I can then login but this error is server output when I try to "users#new" and "users#create" the full error is below, then my users_controller.rb and views/users -> new.html.erb
No template found for UsersController#create, rendering head :no_content
Completed 204 No Content in 35ms (ActiveRecord: 0.5ms)
users_controller.rb
def new
#user = User.new
end
def create
#user = User.new(user_params)
if (#user = User.find_by_email(params[:email]))
flash[:success] = "User already exists."
if #user.save
session[:user_id] = user.id
flash[:success] = "New User created."
redirect_to '/layouts/application'
else
render 'new'
end
end
end
new.html.erb
<h1>Sign Up</h1>
<%= form_with(model: #user) do |f| %>
<p> Username:</br> <%= f.text_field :username %> </p>
<p> Email:</br> <%= f.text_field :email %> </p>
<p> Password:</br> <%= f.password_field :password%></p>
<%= f.submit "Signup" %>
<% end %>
<% if #user.errors.any? %>
<ul class="Signup_Errors">
<% for message_error in #user.errors.full_messages %>
<li>* <%= message_error %></li>
<% end %>
</ul>
<% end %>
</div>
Do I have to have another html.erb file? And how can I tell what that has to be? Sorry for the obvious question, newb here.
As per your code if the User is not present it will not enter in the if block. Rails end up trying to find create.html as the current action is create.
To avoid this you must redirect it somewhere or render a template which you have done in the next if and else but it's not executing.
The condition is not letting it redirect to anywhere. Try moving the if block out like this.
def create
#user = User.new(user_params)
if User.exists?(email: params[:email]) # I think this should be `user_params[:email]` instead of `params[:email]`
flash[:error] = "User already exists."
redirect_to 'whereever/you/want/to/redirect' and return
end
if #user.save
session[:user_id] = user.id
flash[:success] = "New User created."
redirect_to '/layouts/application'
else
render 'new'
end
end

Ruby on Rails - show_user_path NoMethodError in Users#show

I am a begginer in Rails, im following code from a book and i am trying stuff to see if it breaks/works, anyway heres my UserControllers classUserController
class UsersController < ApplicationController
def new
#user = User.new
end
def edit
#user = User.find(params[:id])
end
def show
#user = User.find(params[:id])
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to #user, :notice => 'Cadastro realizado'
else
render :new
end
end
end
And heres my show.html.erb
<p id="notice"><%=notice%></p>
<h2>Perfil: <%=#user.full_name %></h2>
<ul>
<li>Localização: <%= #user.location %> </li>
<li>Bio: <%= #user.bio %></li>
</ul>
<%= link_to 'Editar Perfil', edit_user_path(#user) %>
<%= link_to 'Mostrar Perfil', show_user_path(#user) %>
My problem is in the last line, when i try to acess this page i get a NomethodError,i am trying to understand why, why can i just change that to #user and the page works?
Try:
<%= link_to 'Mostrar Perfil', user_path(#user) %>
or even just
<%= link_to 'Mostrar Perfil', #user %>
In order to see how to name the routes, open a console and run
rake routes

Rails: NoMethodError in Users#show

I'm new to Rails and I've been wandering aimlessly around stackoverflow trying to find a solution to my problem but can't seem to figure it out. I'm doing chapter 10 of Michael Hartl's tutorial and when I try to view the profile of a specific user the localhost:3000 page I get the following error messages:
"NoMethodError in Users#show"
followed by
"undefined method `name' for nil:NilClass".
The source is listed as the first line of my show.html.erb file but I can't see anything wrong with the code.
The home page works fine and the users index is viewable but beyond that it doesn't work. I understand that this could mean that the #user object is nil but I'm not sure how to fix this. My Rspec tests are also failing hard - any help would be greatly appreciated.
My users_controller.rb file:
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:index, :edit, :update, :destroy]
# Arranges for a particular method to be called before the given actions.
before_filter :correct_user, only: [:edit, :update]
before_filter :admin_user, only: :destroy # Restricts the destroy action to admins.
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
sign_in #user
flash[:success] = "Welcome to the Sample App!"
redirect_to #user
else
render 'new'
end
end
def index
#users = User.paginate(page: params[:page])
end
def edit
# #user = User.find(params[:id])
end
def update
# #user = User.find(params[:id])
if #user.update_attributes(params[:user])
flash[:success] = "Profile updated"
sign_in #user
redirect_to #user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_url
end
private
# def signed_in_user
# unless signed_in?
# store_location
# redirect_to signin_url, notice: "Please sign in."
# end
# end
def correct_user
#user = User.find(params[:id])
redirect_to(root_path) unless current_user?(#user)
end
def admin_user
redirect_to(root_path) unless current_user.admin?
end
def show
#user = User.find(params[:id])
#microposts = #user.microposts.paginate(page: params[:page])
end
end
My show.html.erb file:
<% provide(:title, #user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
</h1>
</section>
</aside>
<div class="span8">
<% if #user.microposts.any? %>
<h3>Microposts (<%= #user.microposts.count %>)</h3>
<ol class="microposts">
<%= render #microposts %>
</ol>
<%= will_paginate #microposts %>
<% end %>
</div>
</div>
And users_helper.rb
module UsersHelper
# Returns the Gravatar (http://gravatar.com/) for the given user.
def gravatar_for(user, options = { size: 50 })
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
end
Here #user is nil. Because there is no record with the id (params[:id]). Thats why this error is coming.
<h1>
<%= gravatar_for #user %>
<%= !#user.nil? #user.name : "" %>
</h1>
It will check whether the #user having any record, if its having it will display it's name else it will display empty.
Your query doesn't find a user:
#user = User.find(params[:id])
does apparently return nil.
Please check that a user with the given ID exists. Additionally, you should make your actions fail safely when no object(s) are found:
<% if #user.nil? %>
<% provide(:title, 'Not found') %>
No user with that ID has been found.
<% else %>
<!-- Contents of your current show.html.erb -->
<% end %>

Rails table edit form

My problem is that if I want to test the edit form I always get this exception..
Can you help me figure out the problem?
This is the error message:
undefined method `model_name' for NilClass:Class
Extracted source (around line #5):
2: <div class="row">
3: <div class="box">
4: <span id="logo">Azubiware 2.0</span><br><br>
5: <%= form_for(#bsinfo) do |f| %>
6: <% #basedate = Date.new(#bsinfo.year) %>
7: <% #basedate = #basedate.beginning_of_year %>
8: <% #basedate = #basedate.beginning_of_week %>
I have the same form going with my users table and this works properly...
class BsinfosController < ApplicationController
def index
#title = "Verwaltung Abwesehnheiten"
end
def new
#title = "Sign up"
#bsinfo = Bsinfo.new
end
def show
#bsinfo = Bsinfo.find(params[:id])
#title = #bsinfo.year
end
def create
#bsinfo = Bsinfo.new(params[:bsinfo])
if #bsinfo.save
flash[:success] = "Schedule successfull created"
redirect_to bsinfos_path
else
render 'new'
end
end
def edit
#title = "Settings"
end
def update
if #bsinfo.update_attributes(params[:bsinfo])
flash[:success] = "Profile successfull updated"
redirect_to #bsinfo
else
render 'edit'
end
end
def destroy
Bsinfo.find(params[:id]).destroy
flash[:success] = "Scheduel destroyed"
redirect_to bsinfos_path
end
end
And the link to the edit form is like
<% #bs = Bsinfo.all %>
<% #bs.each do |bsall| %>
<%= link_to "#{bsall.name}", edit_bsinfo_path(bsall), :class => "btn" %>
<% end %>
The url appears like
localhost:3000/bsinfos/17/edit
Whatever model your form is based on is being returned as nil. Make sure the #var in the controller that the form is based on is actually set to the instance of the object you want.
The snippet of code:
form_for(#bsinfo)
is generating your error most likely because #bsinfo is nil.
I'd recommend looking at your controller code and checking for the conditions under which #bsinfo can be nil.

Resources