我有一个表单,其中有一些字段应填充到我的数据库中,而不是。我不确定为什么会这样。我定义了修复另一个错误但我的数据没有被存储的参数。我的表名是用户。
我的控制器:
Class LandlordPageController < ApplicationController
before_action :get_user
def get_user
@user = current_User
end
def index
end
def show
end
def create
@user = User.new(user_params)
@user.save
redirect_to profile_page_index_path
end
private
def user_params
params.require(:user).permit(:address, :cityResiding, :ssn, :birthDate, :gender, :phoneNumber)
end
end
我的表格:
<%= form_for :user do |f| %>
<div class="field">
<%=f.label :address, 'Current Address' %><br/>
<%= f.text_field :address, :required => 'required' %>
</div>
<div class="field">
<%=f.label :cityResiding, 'Current City' %><br/>
<%= f.text_field :cityResiding, :required => 'required' %>
</div>
<div class="field">
<%=f.label :phoneNumber, 'Phone Number'%><br/>
<%= f.telephone_field :phoneNumber, maxlength: 15, :required => 'required' %>
</div>
<div class="field">
<%=f.label :gender, 'Gender'%><br/>
<%= f.select :gender, ['',' Male', 'Female', 'Other','Prefer Not to Answer']%>
</div>
<div class="field">
<%=f.label :birthDate, 'Birth Date'%><br/>
<%= f.date_select :birthDate, order: [:month, :day, :year], :required => 'required'%>
</div>
<div class="field">
<%=f.label :ssn, 'Social Security Number' %><br/>
<%= f.text_field :ssn, maxlength: 9 %>
</div>
<div class="actions">
<%= f.submit "Submit Information" %>
</div>
<% end %>
日志:
Started GET "/landlord_page" for 127.0.0.1 at 2016-11-06 17:59:58 -0500
Processing by LandlordPageController#index as HTML
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
Rendering landlord_page/index.html.erb within layouts/application
Rendered landlord_page/index.html.erb within layouts/application (4.0ms)
Rendered layouts/_navbar.html.erb (1.0ms)
Completed 200 OK in 98ms (Views: 88.4ms | ActiveRecord: 0.0ms)
Started POST "/landlord_page" for 127.0.0.1 at 2016-11-06 18:00:06 -0500
Processing by LandlordPageController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"98tUidqprFyTG8ZC/tV9TRDIVlV0I+ocnQfKTUDqorlS+JMFHtaCWz69EwBvH5MrHhnRbg93m695//Z1I5xt3A==", "user"=>{"address"=>"1", "cityResiding"=>"1", "phoneNumber"=>"1", "gender"=>" Male", "birthDate(2i)"=>"11", "birthDate(3i)"=>"6", "birthDate(1i)"=>"2016", "ssn"=>""}, "commit"=>"Submit Information"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.0ms) begin transaction
(0.0ms) rollback transaction
Redirected to http://localhost:3000/profile_page
Completed 302 Found in 4ms (ActiveRecord: 0.0ms)
编辑:我的表单确实提交了数据,但保存时会抛出此错误
NoMethodError in LandlordPageController#create
undefined method `save' for #<Array:0x0000000d883a08>
答案 0 :(得分:1)
而不是 function toggleClassFunction() {
$('nav').toggleClass('light-mode');
}
var waypoint = new Waypoint({
element: document.getElementById('id-of-element-when-scrolled-to-event-triggers'),
handler: function(direction) {
if (direction === 'down') {
toggleClassFunction(); // the function that runs
this.destroy() //runs the function once
}
else {
}
},
offset: 250 //since the event triggers once the element reaches the top of the page, we
//sometimes need an offset. so in this example, when the element is 250px from
//the top of the page, the even triggers
});
我使用了new
并删除了update
,因为已经执行的新保存参数并保存是尝试保存nil参数。我没有创建 new 用户,但意味着向现有用户添加新参数。这就是发生错误的原因。完整的最终代码如下。
save
答案 1 :(得分:-1)
如前所述,问题可能是验证错误。
如果您要更新用户,则需要使用update
方法并将用户传递给表单
form_for @user
一旦表单提交,这将向控制器上的PATCH
方法发送update
请求。
def update
@user.update user_params
if ! @user.valid?
flash[:error] = @user.errors.full_messages
end
redirect_to profile_page_index_path
end