Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html Rails在提交表单后再次重定向回同一页面_Html_Ruby On Rails_Forms - Fatal编程技术网

Html Rails在提交表单后再次重定向回同一页面

Html Rails在提交表单后再次重定向回同一页面,html,ruby-on-rails,forms,Html,Ruby On Rails,Forms,[更新问题,请参见下半部分] 我正在构建一个简单的web应用程序,它有一个简单的注册/登录表单。 我想要的系统是 在表单中输入您的姓名和关键字。(位于/save_user.html) 按下“确认”链接 检查是否使用了用户名。真->更新,假->注册 重定向到/bye.html(控制器) 现在我每次都用一个新的用户名/关键字集进行测试。 问题是,最终的输出是/save\u update,并以一个错误结束,该错误表示没有给出任何参数。(当然没有给出任何参数) 我能为你做点什么吗?谢谢 控制器 #

[更新问题,请参见下半部分]

我正在构建一个简单的web应用程序,它有一个简单的注册/登录表单。 我想要的系统是

  • 在表单中输入您的姓名和关键字。(位于/save_user.html)
  • 按下“确认”链接
  • 检查是否使用了用户名。真->更新,假->注册
  • 重定向到/bye.html(控制器)
现在我每次都用一个新的用户名/关键字集进行测试。 问题是,最终的输出是
/save\u update
,并以一个错误结束,该错误表示没有给出任何参数。(当然没有给出任何参数)

我能为你做点什么吗?谢谢

控制器

#
# Register user
#
def save_user
  point_id = session[:point_id]
  @point = Point.find(point_id)
end

def save_update
  username = params[:user][:name]
  keyword = params[:user][:key]
  point_id = session[:point_id]
  #
  # if username exists, match password.
  # if not, create new.
  #
  user = User.find_by_username(username)
  if user
    if user.keyword == Digest::SHA1.hexdigest(user.salt + keyword)
      # User exists!
      render text: 'Matched!'
    else
      # keyword is wrong
      render text:"keyword is wrong.."
    end
  else
    # save new user!
    user = User.create(salt:SecureRandom.hex(4))
    user.update(
        username:username,
        keyword:Digest::SHA1.hexdigest(user.salt + keyword)
    )
    point = Point.find(point_id)
    point.update(:user_id => user.id)

    return redirect_to action: :goodbye
  end
end

def goodbye
  # goodbye.html.erb exists
  point_id = session[:point_id]
  point = Point.find(point_id)
  @username = User.find(point.user_id).username
end
视图(save_user.html.erb)



我现在意识到我的代码有两个问题,但尽管它改变了它,但它并没有解决它

  • 在save_user.html.erb中,我有一个
    form_标记({action::save_update}…
    ),还有一个
    submit_link_标记,其中包含相同的操作。因此它两次调用
    save_-upate
    操作

  • 即使我删除了
    {action::save_update}
    ,我也会遇到同样的错误,但这次似乎是这样,因为submit_链接没有发送任何散列

  • 我现在会从头开始重新考虑我的代码,但是如果有人对这个问题有任何想法,谢谢,这将非常有帮助。

    在您的控制器使用中

     redirect_to :back
    
    试试这个

    redirect_to action: :goodbye
    
    不是


    希望这对您有用。

    使用if user……而不是if-if(user=user.find by_username(username))来检查用户是否存在。如果您没有使用表单对象,那么参数将不会用user作为键包装,例如:(
    “user”=>{“name”=>“some_name”,“key”=>“some_key”}.
    ),因此使用
    参数[:user][:name]
    将给出零结果。因此直接使用
    参数[:name]
    instead@Milind谢谢,但这只是Ruby风格的guidline吗?我已经修复了:)@Abhi我认为
    text\u field\u标记(“user[name]”…
    user:{name:“some\u name”,key:“some\u key”}
    ,为了测试,我插入了
    p参数[:user][:name]
    和a
    p参数[:name]
    ,我得到了
    “some\u name”
    nil
    。谢谢,但最终结果是一样的。它确实发布了save\u update GET save\u update GET save\u user GET save\u update并以
    NoMethodError结束(nil:NilClass的未定义方法
    [])`再次说明。可能是您缺少一些需要传递给该方法的参数,请检查一下。。。
     redirect_to :back
    
    redirect_to action: :goodbye
    
    return redirect_to action: :goodbye