【问题标题】:Rails Form_for "First argument in form cannot contain nil or be empty" after logging out(sessions#destroy)退出后Rails Form_for“表单中的第一个参数不能包含nil或为空”(sessions#destroy)
【发布时间】:2017-07-01 08:32:22
【问题描述】:

登录然后注销后,我不断收到表单中的第一个参数不能为 nil 或为空的错误。如何解决此问题,同时在用户登录时仍保持表单消失?

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    <ul class="nav navbar-nav navbar-left">
      <li>
        <% if current_user %>
        <li>
          <li><%= link_to "Profile Summary", current_user %></li>
        </li>
        <li>
          <li><%= link_to "Edit personal details", edit_user_path %></li>
        </li>
        <li>
          <li><%= link_to "Logout", logout_path, method: "delete" %></li>
        </li>
        <% else %>
        <li><%= form_for @user do |f| %>
          <div class="form-group">
            <%= f.text_field :email, :placeholder => "Email", class: "form-control" %>
          </div>
          <div class="form-group">
            <%= f.password_field :password, :placeholder => "Password", class: "form-control"  %>
          </div>
          <%= f.submit 'Login', class: "btn btn-default" %>
          <% end %>
        </li>
      <% end %>
        <li>
          <li><%= link_to "New User", new_user_path %></li>
        </li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li>
          <li><%= link_to 'Opinions', profile_path %></li>
        </li>
        <li>
          <li><%= link_to 'About',contact_path %></li>
        </li>
        <li>
          <li><%= link_to 'Contact', contact_path %></li>
        </li>
      </ul>
    </div>
    <!-- /.navbar-collapse -->
  </div>

以下是我的用户控制器

      def new
@user = User.new
  end
def show
@user = User.find_by_id(session[:user_id])
end
def create
@user = User.new(user_params)
if @user.save
  flash[:success] = "Account Created. Please Login"
  redirect_to profile_path
else
  flash[:danger] = "Please fill in all your information"
  render :new
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update(user_params)
  redirect_to @user
  flash[:success] = 'User profile successfully updated.'
else
  render :edit
end
end
def profile
end

这是会话控制器

    def create
user = User.find_and_authenticate_user(user_params)

if user
  session[:user_id] = user.id
  flash[:success] = "You are logged in."
  #redirect_to refers to a get command by default as it is
  #the only command to display. *_path is a combination of
  #the RESTful actions
  redirect_to profile_path
else
  flash[:danger] = "Credentials Invalid!!"
  redirect_to login_path
end
end

def destroy
session[:user_id] = nil
flash[:success] = "User logged out!!"
redirect_to root_path
end

【问题讨论】:

  • 为什么不尝试使用 form_tag 并传递电子邮件和密码并验证/创建用户。
  • 试试&lt;%= form_for resource, as: resource_name do |f| %&gt;
  • 抱歉,只是对原始代码进行了轻微编辑。我还要试试 form_tag 吗?
  • 你需要告诉我们你是如何在你的控制器中定义@user的......因为你的用户实例变量在注销时是零

标签: ruby-on-rails form-for


【解决方案1】:

由于您没有描述您的 current_user 辅助方法,因此有点难以回答。

但是,根据我的理解,current_user 方法将给登录的用户,而不是给一个布尔值。

如果是这种情况,对于新用户,show action 将不会收到任何会话 ID,因此,@user 将为空。现在,您的条件将失败,else 部分将为您提供 null 或空错误。

要克服这个问题,您可以在您的辅助方法中比较 session[:user_id] == @user.id 并返回布尔值作为结果,或者如果辅助返回完整的登录用户对象,则在您的视图中使用会话 ID 直接比较。

希望这会有所帮助..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    相关资源
    最近更新 更多