我的 config / routes.rb
中包含此代码Rails.application.routes.draw do
get 'timerecords/index'
get 'employees/index'
get 'employees/show'
get 'employees/new'
get 'welcome/index'
root :to => 'employees#new'
resources :employees do
get '/update_info' => 'employees#update_info'
end
resources :timerecords
end
当我尝试保存新记录时,我的 app / controllers / employees_controller.rb
def create
fname = params[:fname]
mname = params[:mname]
lname = params[:lname]
contactno = params[:contactno]
address = params[:address]
username = params[:username]
password = params[:password]
status = params[:status]
@employee= Employee.create(fname: fname, mname: mname, lname: lname,
contactno: contactno, address: address,
username: username, password: password, status: status)
redirect_to @employee
end
使用上面的代码,但在类名
下skip_before_filter :verify_authenticity_token
现在,我在同一控制器下更新
def update_info
fname = params[:fname]
mname = params[:mname]
lname = params[:lname]
contactno = params[:contactno]
address = params[:address]
username=params[:username]
password=params[:password]
status=params[:status]
id=params[:id]
@employee = Employee.find(id)
@employee.update_attributes(id: id, fname: fname, mname: mname,
lname: lname, contactno: contactno, address: address,
username: username, password: password, status: status)
redirect_to @employee
end
当我输入链接时,它会更新很好但有额外的数字。链接看起来像这样
> / 0 /没有任何意义,但是当我没有意义时,更新不会发生。我在这里尝试做的是调用我的vb.net代码中的链接使用POST方法更新,但我无法更新。保存很顺利,但在更新期间没有。保存时这是我的vb.net代码
strLink = "https://dtitdtr.herokuapp.com/employees"
strData = "fname=" + UrlEncode(txtFName.Text.Trim.ToUpper()) + "&mname=" + UrlEncode(txtMName.Text.Trim.ToUpper()) _
+ "&lname=" + UrlEncode(txtLName.Text.Trim.ToUpper()) + "&contactno=" + UrlEncode(txtContactNo.Text.Trim) _
+ "&address=" + UrlEncode(txtAddress.Text.Trim.ToUpper()) + "&username=" + UrlEncode(txtUsername.Text.Trim.ToUpper()) _
+ "&password=" + UrlEncode(Encrypt(txtPassword.Text)) + "&status=" + UrlEncode("AC")
Try
Save(strLink, strData)
MessageBox.Show("Your registration has been successful!", _
"Registered!", MessageBoxButtons.OK, MessageBoxIcon.Information)
cmdClear_Click(sender, e)
frmMenu.Show()
Me.Hide()
Catch ex As Exception
MsgBox(ex.Message)
End Try
模块
Public Sub Save(ByVal link As String, ByVal data As String)
request = WebRequest.Create(link)
request.Credentials = CredentialCache.DefaultCredentials
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
postData = String.Format(data)
request.ContentLength = postData.Length
byteArray = Encoding.ASCII.GetBytes(postData)
request.ContentLength = byteArray.Length
dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
response = request.GetResponse()
dataStream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
reader = New StreamReader(dataStream)
webRep = (CType(response, HttpWebResponse).StatusDescription)
dataStream = response.GetResponseStream()
reader = New StreamReader(dataStream)
reader.Close()
response.Close()
End Sub
我尝试了更新,但它只是没有工作。
request = WebRequest.Create("http://localhost:3000/employees/40/update_info?")
不会发生更新...好像它没有执行...如果用户名不存在,即使我尝试更新信息而不添加,它也会立即添加。 我想问题出在我的 RAILS 代码中。我无法理解什么。 任何人都可以帮助我吗?
答案 0 :(得分:0)
我想出了这个答案......我改变了我的路线.rb到了这个
Rails.application.routes.draw do
get 'timerecords/index'
get 'employees/index'
get 'employees/show'
get 'employees/new'
get 'welcome/index'
root :to => 'employees#new'
post '/update_info'
resources :employees, :timerecords
end
以这种方式拥有我的vb代码......
request = WebRequest.Create("http://localhost:3000/employees/update_info")
并确保在元素中添加 id 。 :)