flash[:notice] gets erased as soon as I use redirect_to in rails 2.2.2
It's a super simple ruby application, so it shouldn't be to hard to find what's going on (although, I couldn't find it obviously...)
My routes.rb looks like this
resources :happenings
root to: "happenings#index"
post "/happenings/save"
And my HappeningsController looks like this
def index
#happenings = Happening.all
end
def save
flash[:notice] = "saved"
redirect_to root_path
end
Now if I'm debugging and I add puts flash[:notice] to the save action, it will print "saved" to my terminal
if I add `puts flash[:notice] to the index action, it will print an empty line to the terminal. So it obviously has been erased by the time the index action gets executed.
Any ideas? Thanks a lot?
Edit, Here's a bit more information.
The save action get's called by a form
<form method="post" action="/happenings/save">
The following is the server's output when I submit a form (and request the save action)
Started POST "/happenings/save" for 127.0.0.1 at 2015-07-02 23:16:31 +0200
Processing by HappeningsController#save as HTML
Parameters: {"happening_name"=>"Test happening", "happening_time"=>"2015-07-24T13:15"}
Can't verify CSRF token authenticity
Redirected to http://localhost:3000/
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2015-07-02 23:16:32 +0200
Processing by HappeningsController#index as HTML
Rendered happenings/index.html.erb within layouts/application (0.1ms)
Completed 200 OK in 32ms (Views: 28.4ms | ActiveRecord: 0.0ms)
https://stackoverflow.com/a/12340941/2011580
If you are having problem to keep the flash after redirecting to another path, then use this.
flash.keep
in your method, before redirecting.
You might want to do something like this:
def save
redirect_to root_path
flash[:notice] = "saved"
end
And then place
<%= flash[:notice] %>
anywhere in your index.html.erb.
Can you make sure you aren't reseting any cookie from application controller? Because flash messages are stored in cookie. You can also try
flash.now[:notice] = '....'
before redirection you can use:
flash.keep(:notice)
source: http://guides.rubyonrails.org/action_controller_overview.html
A tip:
You can install a gem named pry. You can set that in your view file
when you need, so you can get an interactive shell in your server when
the server serve that page. There you can test all your dynamic value
carriers. And you can understand whats going on actually.
Related
NOTE: I'm not using Devise.
I'm implementing a password reset action and I have the controller Password_Resets with the following edit action:
def edit
end
def update
puts "I'm in the update!!"
if params[:user][:password].empty?
#user.errors.add(:password, "can't be empty")
render 'edit'
elsif #user.update_attributes(user_params)
log_in #user
flash[:success] = "Password has been reset."
redirect_to recipes_url
else
render 'edit'
end
end
When I'm trying to run through it, I put in the appropriate URL and I get the following response:
Started GET "/password_resets/igArFj9sYLt1J6k6Y2BjSg/edit?email=maggie%40example.com" for ::1 at 2016-04-20 21:49:58 -0500
Processing by PasswordResetsController#edit as HTML
Parameters: {"email"=>"maggie#example.com", "id"=>"igArFj9sYLt1J6k6Y2BjSg"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."email" = ? LIMIT 1 [["email", "maggie#example.com"]]
Redirected to http://localhost:3000/
Filter chain halted as :valid_user rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.1ms)
Why is it redirecting to localhost rather than the view app/views/password_resets/edit?? I can't seem to figure out a good way to "debug" this or see what's going on.
NOTE: I made that view incredibly simple to make sure it wasn't redirecting.
This is the view app/views/password_resets/edit:
<h1>This is my edit view </h1>
EDIT::: SOLUTION
So I was just an idiot. Essentially I have a validated user command that checked if the user was authenticated and the user was not. Therefore I need some more error handling.
Thanks you!!
The error is in your validations:
Filter chain halted as :valid_user rendered or redirected
I'm assuming you are calling this at the top of your controller
You can see in your web server logs:
Filter chain halted as :valid_user rendered or redirected
It's mean you have before_filter or before_action in a controller.
This filter is just ruby method - def valid_user and obvious that it contains a condition with redirect.
You need to debug this method.
You can read more about controller filters here
Im working on my Rails Backend in Ruby and i want to post Data to this server. But if i do a Post-request with PAW i get redirected. Im a newbie to Http Requests. Could someone explain me the functionality and how to use http post requests?
i want to post information on my server's datanase (sqlite3).
Here's a screenshot which should explain everything:
how does this work? please explain :)
thanks.
greetings John
and here's the code:
OwnersController:
#app/controllers/owners_controller.rb
class OwnersController < SessionsController
respond_to :html
before_action :owner_find, only: [:show, :edit, :update, :destroy]
def index
#owners = Owner.all
end
def show
end
def update
#owner = Owner.find(params[:id])
if #owner.update(owner_params)
redirect_to #owner
else
render 'edit'
end
end
def new
#owner = Owner.new
end
def destroy
#owner.destroy
redirect_to owners_path
end
def edit
end
def create
#owner = Owner.new owner_params
if #owner.save!
flash[:notice] = 'You signed up successfully'
flash[:color]= 'valid'
redirect_to owners_path
else
flash[:notice] = 'Form is invalid'
flash[:color]= 'invalid'
render 'new'
end
end
private
def owner_find
#owner = Owner.find(params[:id])
end
def owner_params
params.require(:owner).permit(:name, :password, :password_confirmation, :token)
end
end
SessionController:
class SessionsController < ApplicationController
before_filter :authenticate_user, :except => [:login, :login_attempt]
def login
#goes to Login Form
end
def logout
session[:owner_id] = nil
redirect_to :action => 'login'
end
def login_attempt
authorized_user = Owner.authenticate_by_name(params[:login_name],params[:login_password])
if authorized_user
session[:owner_id] = authorized_user.id
flash[:notice] = "Wow Welcome again, you logged in as #{authorized_user.name}"
redirect_to welcome_index_path
else
flash[:notice] = 'Invalid Username or Password'
flash[:color]= 'invalid'
render 'login'
end
end
end
Console Logs:
from web-request (http://192.168.2.144:3000/owners?name=hans&password=hans321&password_confirmation=hans321)
Started GET "/owners?name=hans&password=[FILTERED]&password_confirmation=[FILTERED]" for 192.168.2.144 at 2015-10-01 12:12:18 +0200
Cannot render console from 192.168.2.144! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by OwnersController#index as HTML
Parameters: {"name"=>"hans", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}
Owner Load (0.1ms) SELECT "owners".* FROM "owners" WHERE "owners"."id" = ? LIMIT 1 [["id", 2]]
Owner Load (0.1ms) SELECT "owners".* FROM "owners"
Rendered owners/index.html.erb within layouts/application (1.8ms)
Completed 200 OK in 60ms (Views: 58.9ms | ActiveRecord: 0.2ms)
It's telling 200 ok but nothing happens in the DB.
from Paw-Request (so i can use post. btw. how do i use post in browser request?
Started POST
"/owners?name=hans&password=[FILTERED]&password_confirmation=[FILTERED]"
for 192.168.2.144 at 2015-10-01 12:12:45 +0200 Cannot render console
from 192.168.2.144! Allowed networks: 127.0.0.1, ::1,
127.0.0.0/127.255.255.255 Processing by OwnersController#create as HTML Parameters: {"name"=>"hans", "password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"} Can't verify CSRF token
authenticity Redirected to http://192.168.2.144:3000/ Filter chain
halted as :authenticate_user rendered or redirected Completed 302
Found in 1ms (ActiveRecord: 0.0ms)
It seems that the CRSF authentication failed..
Edit:
at first:
to Rich Peck! This helped me so much. Thank you!! I really appreciate your effort.
Im near to the solution.. My problem is: i cant put the correct params in the url. The token-auth is disabled for testing. so it wont matter.
the params should be like:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"q9JvFhoSUgfydFTvh18JHbIIdKNDjnOS9m/trVBu9EHPP04xGsO69zPh1BFZBI1Ev1YcnOTiPmaAiPWOSkm5Xg==", "owner"=>{"name"=>"Hubert", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create Owner"}
and not as in my request:
Parameters: {"name"=>"Hubert", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "owner"=>{}}
HTTP Status Codes
Firstly, a 30x response means "Resource Moved".
301 responses are used by many SEO people to denote permanent relocation of resources. 302 not so common, but still means a similar thing.
Every time you send & receive HTTP requests, you're going to receive a status code. The typical is the 200 response -- status success!
What you're seeing is the redirect_to command in action -
if #owner.save!
flash[:notice] = ...
redirect_to owners_path
I've never used PAW before, but I assume it's just giving you the pure response of the server, which would in this case be a 30x "Resource Moved" code.
I would expect a typical browser request to load the redirected route and display its yield on the screen.
Server
As a way to test this, you should attempt the same transaction in your browser:
lvh.me:3000/orders
(lvh.me is a domain routed to your own localhost which helps with subdomains in Rails)
This will give you the ability to test and see what happens with the responses. You *should * find that your data has been saved to the database (albeit SQLite3 in your case).
Syntax
Finally, you need to ensure you're using the correct syntax in your code.
Specifically:
#app/controllers/owners_controller.rb
class OwnersController < ApplicationController
...
def create
#owner = Owner.new owner_params
end
private
def owner_params
params.require(:owner).permit(:name, :password, :password_confirmation)
end
end
You'll also want to look at bcrypt-ruby for protecting your passwords.
Testing
I tend to just test my Rails apps with standard browser functionality.
This means you can run the Rails Server ($ rails s in your console), which you'll then be able to then access through your browser.
You're trying to use this PAW thing, which is okay, but doesn't give you much flexibility in regard to the user-interactivity of the app (for example, submitting real forms etc)...
In your case, I'd do the following:
#app/views/orders/new.html.erb
<%= form_for #order do |f| %>
<%= f.text_field :name %>
<%= f.password_field :password %>
<%= f.password_field :password_confirmation %>
<%= f.submit %>
<% end %>
You'd then access lvh.me:3000/orders/new and submit the form. This will show you how it responds!
HTTP
Okay here's the deal with HTTP requests...
Whenever you send a piece of transactional data to your web application, you do it through an HTTP request. HTTP requests are just a way to send data through the "Internet".
With Rails based apps, this means that every time you "do" something in the app, you're really sending an HTTP request to your web server. Rails interprets this request and sends a response. This response is what your question is about.
You're asking about receiving 302 responses - this is the web server's way of saying you've been redirected. It's pretty basic stuff to be honest; your browser handles most of it.
A great tutorial can be found here:
Alright then your error is as follows:
Can't verify CSRF token authenticity
I can elaborate more on this later, but for now, you might want to look up this solution: WARNING: Can't verify CSRF token authenticity in case of API development
on my site, I'm checking to see if the user has been authenticated. I have in my controller the following..
def index
login_required
#profile = current_user.profile ### line 9
..
Then in my application_controller, I have the definition of the login_required method..
def login_required
unless current_user
redirect_to root_path, :notice => "Please login first"
end
end
Even though my log shows me that the redirection to root is happening, it's still not really taking me to the root path, and rather takes me back to my controller->index
and errors out on the line "#profile = current_user.profile". here's the log..
Started GET "/meetings" for 127.0.0.1 at 2012-12-05 12:18:12 -0800
Processing by xyzController#index as HTML
Redirected to http://localhost:3000/
Completed 500 Internal Server Error in 1ms
NoMethodError (undefined method `profile' for nil:NilClass):
app/controllers/xyz_controller.rb:9:in `index'
Why am I not being redirected to the root path (which is my home page) even though it's saying that it's redirecting? Any ideas? Thanks.
Processing keeps happening after redirect_to - there's no implicit return. The band-aid fix is to add a return in your index action depending on the result of login_required, but the better way is to use a filter in your controller.
I'm running integration tests to make sure my before_filter is redirecting to root_path if the user is not logged in. Everything seems to be working fine, but I'm seeing http://www.example.com/ in the Redirected to message in log/test.log, and I'm just wondering if this is normal, or if there's a configuration that I missed somewhere. Thanks!
log/test.log
Started PUT "/users/980190963" for 127.0.0.1 at 2012-07-29 13:31:39 -0700
Processing by UsersController#update as HTML
Parameters: {"user"=>{"password"=>"[FILTERED]"}, "id"=>"980190963"}
Redirected to http://www.example.com/
Filter chain halted as :reject_if_logged_out rendered or redirected
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)
users_controller.rb
...
before_filter :reject_if_logged_out, only: [:update]
...
private
def reject_if_logged_out
redirect_to root_path unless #current_user
end
Strangely enough, only some of my apps seems to do this. But this has been answered here before, it's the default and here's how to change it: How do I change the default "www.example.com" domain for testing in rails?
Because that's the default in in capybara, the library that cucumber and many other testing frameworks for rails use. See lib/capybara.rb:
Capybara.configure do |config|
# ...
config.default_host = "http://www.example.com"
# ...
end
So if you do not specify anything else in your configuration the default value for #request.host is www.example.com.
Using Rails 3.0.9, I'm trying to redirect after a post to the create method in UserController, Firebug shows the POST is sent correctly, my create method is executed, POST returns '302' moved temporarily. As expected the, GET request routes to the new action of the UserProfileController, HOWEVER, there is not response to the GET request!
I tried this with other controllers/actions also with the same result. Why is the GET request after redirect empty? What am I doing wrong? A standard request to /users/profile/new does return the expected GET response.
UserController
def create
#user = User.new(params[:user]) if params[:user]
logger.debug (params[:user])
logger.debug #user
logger.debug #user.valid?
logger.debug #user.errors
if(#user.valid?)
session[:new_user] = #user
logger.debug "Creating User Profile"
end
redirect_to new_user_profile_url # controller => user_profile, action => new
end
UserProfileController
def new
#user_profile = UserProfile.new
# check to see if the new user is in session, if not, then redirect to previous page
user = session[:new_user]
logger.debug 'going to right url???'
logger.debug request.method
logger.debug request.format
logger.debug request.headers
logger.debug request.url
respond_to do |format|
format.html
format.xml { render :xml => #user_profile }
end
end
Server Log
Creating User Profile
Redirected to http://localhost:3000/users/profile/new
Completed 302 Found in 325ms
Started GET "/users/profile/new" for 127.0.0.1 at Fri Aug 05 10:38:38 -0500 2011
Processing by UserProfileController#new as HTML
going to right url???
GET
text/html
http://localhost:3000/users/profile/new
Rendered shared/_trust_reports.haml (45.0ms)
Rendered user_profile/new.haml within layouts/application (590.0ms)
Completed 200 OK in 973ms (Views: 616.0ms | ActiveRecord: 0.0ms)
Turns out it was my own mistake, I did not realize the javascript/theme we were using defaulted to submit all forms via ajax. After changing the default behavior, it worked as expected, thanks for the suggestion Fabio.
According to that log file the server has ended its job and has rendered the output. It could be a problem with your browser (sometimes firebug and other developer tools lock the browser) or it could be a problem with the webserver. I'm assuming you're using webrick (the default one).
Please try your code with another browser and if the problem remains try with a different rails server, there are a lot of them: passenger, unicorn, mongrel, thin.
Usually I develop with passenger standalone, you could install it with gem install passenger and then from your rails root simply run
passenger start
The first time it will take some minutes because it will download nginx standalone and it will compile it.