我想要在我的服务器数据库(sqlite3)上发布信息。
这里有个截图,应该能解释一切:
![i hope thats all information you need](https://istack.dev59.com/2l0qo.webp)
以下是代码:
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
会话控制器:
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
控制台日志:
来自 web-request (http://192.168.2.144:3000/owners?name=hans&password=hans321&password_confirmation=hans321)
开始GET请求"/owners?name=hans&password=[FILTERED]&password_confirmation=[FILTERED]",来自192.168.2.144的请求时间为2015-10-01 12:12:18 +0200 无法从192.168.2.144呈现控制台!允许的网络:127.0.0.1,::1,127.0.0.0/127.255.255.255 处理OwnersController#index作为HTML格式 参数:{"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" 在layouts/application中呈现了所有者/index.html.erb(1.8ms) 完成200 OK,用时60ms(视图:58.9ms | ActiveRecord:0.2ms)它显示200 ok,但在数据库中没有发生任何事情。
来自Paw-Request(所以我可以使用post。顺便问一下,在浏览器请求中如何使用post?
开始POST请求 "/owners?name=hans&password=[FILTERED]&password_confirmation=[FILTERED]",来自192.168.2.144,时间为2015-10-01 12:12:45 +0200。无法从192.168.2.144渲染控制台!允许的网络:127.0.0.1、::1、127.0.0.0/127.255.255.255。OwnersController#create处理HTML格式,参数为{"name"=>"hans", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}。无法验证CSRF令牌真实性。重定向到http://192.168.2.144:3000/。过滤器链因身份验证用户呈现或重定向而停止。在1毫秒内完成302发现(ActiveRecord: 0.0ms)。看起来CSRF认证失败了...
编辑:
首先,感谢Rich Peck!这对我帮助很大。谢谢!我非常感激你的努力。
我接近解决方案了...我的问题是:我无法在URL中放置正确的参数。令牌身份验证已被禁用进行测试,因此不会有影响。
参数应该像这样: Parameters: {"utf8"=>"✓", "authenticity_token"=>"q9JvFhoSUgfydFTvh18JHbIIdKNDjnOS9m/trVBu9EHPP04xGsO69zPh1BFZBI1Ev1YcnOTiPmaAiPWOSkm5Xg==", "owner"=>{"name"=>"Hubert", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create Owner"}
而不是我的请求中这样: Parameters: {"name"=>"Hubert", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "owner"=>{}}