簡體   English   中英

參數缺失或值為空:usuario

[英]param is missing or the value is empty: usuario

我正在學習Rails及其在Web編程的某些方面如何工作。 我知道這個問題已經被問了很多次,比如這一個 ,但是當我試圖復制這個問題的答案我不斷收到同樣的錯誤。 你能幫我么?

usuarios_controller.rb:(創建)

def create
  @usuario = Usuario.new(user_params)

  if @usuario.save
    redirect_to root_path, notice: "The person has been created!" and return
  end
  render 'new'
end

user_params:

private
  def user_params
   params.require(:usuario).permit(:user, :pass)
  end

我正在使用的HTML代碼創建:

<h1>New Person</h1>

<body>
    <%= form_tag "/usuarios/create", :method => "post" do %>
    <p>
    <%= label_tag :user, "Usuario: " %> <br>
    <%= text_field_tag :user %> 
    </p>
    <p>
    <%= label_tag :pass, "Password: " %> <br>
    <%= password_field_tag :pass %> 
    </p>
    <%= submit_tag "Search" %>
  <% end %>

  <% if ENV["RAILS_ENV"] == "development" %>
    <%= debug(params) %>
  <% end %>
</body>

請在view new.html.erb上更新您的表單

<h1>New Person</h1>

<body>
    <%= form_for @usuario do |f|%>
    <p>
    <%= f.label :user, "Usuario: " %> <br>
    <%= f.text_field :user %> 
    </p>
    <p>
    <%= f.label :pass, "Password: " %> <br>
    <%= f.password_field :pass %> 
    </p>
    <%= submit_tag "Search" %>
  <% end %>

  <% if ENV["RAILS_ENV"] == "development" %>
    <%= debug(params) %>
  <% end %>
</body>

並添加您的控制器

def new
  @usuario = Usuario.new()
end

該表單以{"user"=>"abc", "pass"=>"123"}的形式生成參數,而您的user_params方法期望{"usuario"=> {"user"=>"abc", "pass"=>"123"}} 您可以更改表單或更改user_params方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM