HTTP status code 302

故事扮演 提交于 2019-12-04 08:43:21
Richard Peck

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!