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
Related
I'm very new to RoR. I'm trying to learn.
This is my user_controller update/edit part.
def edit
binding.break
#user = User.find(params[:id])
end
def update
#user = User.find(params[:id])
#binding.break
if #user.update(params.require(:user).keep_if{|key| #user.attributes[key] != params[:user][key]}.permit(:username, :email, :password))
# Call to debugger
flash[:notice] = "Article was updated successfully."
redirect_to #user
else
#binding.break
flash[:notice] = "Article was not updated successfully."
render 'edit'
end
end
This is my edit.html.erb file
<h1>Edit an existing User</h1>
<% if #user.errors.any? %>
<h2>The following errors prevented the user from being saved</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<%= puts msg%>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= form_with(model: #user, local: true) do |f| %>
<p>
<%= f.label :username %><br/>
<%= f.text_field :username %>
</p>
<p>
<%= f.label :email %><br/>
<%= f.text_field :email %>
</p>
<p>
<p>
<%= f.label :password %><br/>
<%= f.password_field :password %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
The success case is working fine.
For the negative case where the update fails, I wanted to print the error messages for which the update failed and render it. But currently it just renders the edit page again. Also the #user in the else part of the controller is having the values which is invalid and those are getting filled in the edit page upon rendering. I want to reset to original values and show the errors for which it got failed.
The errors can be anything like, email format not correct or something.
Server logs says
↳ app/controllers/users_controller.rb:19:in `update'
TRANSACTION (0.2ms) rollback transaction
↳ app/controllers/users_controller.rb:19:in `update'
Rendering layout layouts/application.html.erb
Rendering users/edit.html.erb within layouts/application
Username has already been taken
Rendered users/edit.html.erb within layouts/application (Duration: 23.0ms | Allocations: 2337)
Rendered layout layouts/application.html.erb (Duration: 59.2ms | Allocations: 4962)
Completed 200 OK in 152ms (Views: 87.9ms | ActiveRecord: 5.0ms | Allocations: 11120)
Can someone shed some light on this?
render 'edit' with render edit without controller, you need to call #user = User.find(params[:id]) before render to get original value.
If you want to show errors message, grant it to other const and render it in views
def edit
binding.break
#user = User.find(params[:id])
end
def update
#user = User.find(params[:id])
#binding.break
if #user.update(params.require(:user).keep_if{|key| #user.attributes[key] != params[:user][key]}.permit(:username, :email, :password))
# Call to debugger
flash[:notice] = "Article was updated successfully."
redirect_to #user
else
#binding.break
#errors = #user.errors
#user = User.find(params[:id])
flash[:notice] = "Article was not updated successfully."
render 'edit'
end
end
in view, using #errors.full_messages
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
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
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
So let's say I have code like this:
class PostsController < InheritedResources::Base
# Add before_filter here and devise should handle the redirection if the user is not signed in.
before_filter :authenticate_user!, only: [:vote_up]
def vote_up
begin
current_user.vote_for(#post = Post.find(params[:id]))
redirect_to [#post]
flash[:success] = "You have voted successfully"
rescue ActiveRecord::RecordInvalid
redirect_to [#post]
flash[:error] = "You have already voted"
end
end
end
Neither of the messages "You have voted successfully" or "You have already voted" are being shown.
In my view I have:
enter code here
<p id="notice"><%= notice %></p>
<%= #post.embed .html_safe %>
<header>
<h7><%= #post.name %></h7>
</header>
<h8><%= #post.title %></h8>
<article>
<%= #post.content .html_safe %>
<p>
<%= link_to 'Back', posts_path %> |
<%= link_to('Vote for this song!', vote_up_post_path(#post), :method => :post) %></p>
<p><id="notice"><%= notice %></p>
No dice. I'm still not getting flash messages anywhere. Any idea what I'm doing wrong?
You have used
flash[:success] = "You have voted successfully"
in your controller and you have called
<%= notice %>
in your views. You have to change either in your controller or in your views.
You can add
format.html { redirect_to #post, notice: 'You have voted successfully.' }
in your controller and you will get the message that you require.
You should set the flash before redirect_to
And in your view
<p>
<%= flash[:success] unless flash[:success].blank? %>
<%= flash[:error] unless flash[:error].blank? %>
</p>
You can check this link as well
The best way to do it
<% if flash[:notice] %>
<div class="notice"><%= flash[:notice] %></div>
<% end %>
Your code is like: <p><id="notice"><%= notice %></p>
You need <p id="notice"><%= notice %></p>