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
Related
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
I have created a cart in the products view and want to display an error message
views/products/index.html.erb
<% if #order.errors.any? %>
<div class="error error-success note-shadow">
<% #order.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
</div>
<% end %>
controllers/orders_controller.rb
def create
#order = Order.new(params_slip)
respond_to do |format|
if #order.save
format.html {
redirect_to :back,
notice: 'Order was successfully placed.'
}
else
format.html { redirect_to :back }
end
end
end
I am using the above method trying to display the error messages, but it didn't work out. I supposed that is because my create action is in orders_controller, and I couldn't add the error message to the products view?
The root cause is the #order instance variable is not being carried through the redirection process, so that's the reason you don't see any error messages.
You can put the error messages into flash container
if #order.save
[do something]
else
flash[:order_errors] = #order.errors.full_messages
redirect_to :back
end
<% if flash[:order_error] %>
[display it here]
<% end %>
I'm not sure why you put your form in index.html.erb. You'd better put your form in your new.html.erb.
Try to change:
format.html { redirect_to :back }
Into:
render "index"
I hope this helps you
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
In one controller I have
flash.keep[:notice]= "Your password has been updated successfully."
redirect_to organisation_show_url
In application layouts file I have
<% if flash[:notice] %><div class="notice"><%= flash[:notice] %></div><% end %>
But no flash message is displayed on the organisation_show page, what did I do wrong
In your controller
redirect_to organisation_show_url, :notice => "Your password has been updated successfully."
In application layout
<% if notice %>
<%= notice %>
<% end %>
I have a comment resource nested in a post resource.
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.build(params[:comment])
#comment.ip = request.remote_ip
if #comment.save
redirect_to post_path(#post, notice: "Comment was successfully created")
else
flash[:alert] = "Comment could not be created"
render 'posts/show'
end
end
This all works well enough, but I have a nag item in that when the posts/show page with the comment form re-renders, it shows the comment that didn't pass validation inline. I'd like to know the correct way to do this, short of performing some logic in the view layer to not display a comment that isn't saved.
I ended up solving it in the view, because I couldn't find any other solution
<% #post.comments.each do |c| %>
<% unless c.new_record? %>
<strong><%= "#{c.name} wrote: " %></strong><br />
<blockquote><%= c.body %></blockquote>
<% end %>
<% end %>