没有使用form_for将params传递给action

时间:2016-12-06 10:16:27

标签: ruby-on-rails ruby

enter image description here我无法弄清楚为什么我的表单没有将params传递给注册控制器。我有一个注册表单,使用jquery和javascript作为登录表单。我的第二种形式(最后一种)应该通过注册信息发送到注册控制器。不幸的是,我持续得到错误的参数数量(0表示1)。我不确定我是如何不传递信息的,任何帮助都表示赞赏。

new.html.erb
    

<div id="imageDisplay"></div>

<div class="login-switch">
  <div id="login-div" class="button button-outline button-calm switches active-button">
    LOG IN
  </div>
  <div id="signup-div" class="button button-outline button-calm switches">
    SIGN UP
  </div>
</div>

<%= form_for(@user, url: sessions_path, html: {id: "sign-in-form"}) do |f| %>
  <div class="list list-inset">

    <label id="sign-up-input" class="item item-input">
      <%= f.email_field :username, autofocus: true %>
    </label>
    <label id="sign-up-input" class="item item-input">
      <%= f.password_field :password, autocomplete: "off" %>
    </label>

    <%= f.submit "Log in", class: "button button-block button-calm" %>
  </div>
<% end %>


<%= form_for(@user, url: registrations_path, html: {id: "sign-up-form"}) do |f| %>

  <div class="list list-inset">
    <div id="part-1">
      <!-- part one of sign up -->
      <label id="sign-up-input" class="item item-input">
        <%= f.email_field :email, autofocus: true, placeholder: "E-mail" %>
      </label>

      <label id="sign-up-input" class="item item-input">
        <%= f.password_field :password, autocomplete: "off", placeholder: "Password" %>
      </label>

      <label id="sign-up-input" class="item item-input">
        <%= f.password_field :password_confirmation, autocomplete: "off",placeholder: "Confirm password" %>
      </label>

      <div id="slide-part-2" class="button button-block button-calm">Next</div>

    </div>

    <div id="part-2">
      <!-- part two of sign up -->
        <%= f.file_field :avatar, id: "fileInput"%>

      <label id="sign-up-input" class="item item-input">
        <%= f.text_field :username, autofocus: true, placeholder: "username" %>
      </label>

    <%= f.submit "Sign up", class: "button button-block button-calm" %>

    </div>
<% end %>

  </div>

</div>

registrations_controller.rb

class RegistrationsController < ApplicationController
[![enter image description here][1]][1] def new
    end

    def create
        @user = User.create(sign_up_params)
        if @user.save
            log_in @user
            flash[:success] = "We Made it!"
        else
            flash[:error] = @user.errors
        end
    end

    private 

    def sign_up_params
        params.require.(:user).permit(:email, :username, :avatar, :password, :password_confirmation, :trump)
    end

end

3 个答案:

答案 0 :(得分:3)

.之后删除params.require

def sign_up_params
  params.require(:user).permit(:email, :username, :avatar, :password, :password_confirmation, :trump)
end

答案 1 :(得分:1)

应该是这样的:

 def sign_up_params
        params.require(:user).permit(:email, :username, :avatar, :password, :password_confirmation, :trump)
 end

答案 2 :(得分:1)

错误显示为wrong number of arguments(0 for 1),因为您没有在require方法中传递任何参数,而是在开头括号前添加了.参数,你需要删除它,因为Ruby正在执行(:user)作为方法,但事实并非如此。修正错误,将错误更正为答案

中提到的 dp7
def sign_up_params
  params.require(:user).permit(:email, :username, :avatar, :password, :password_confirmation, :trump)
end

希望有所帮助!