存在从数据库中正确提取并显示的表单。问题是,当我尝试更新表单时,不是在user_id =用户ID的地方更新,而是在user_id等于行ID的地方更新。
Edit Function
@home = Home.find_or_initialize_by(user_id: current_user.id)
Form Page
<% if local_assigns[:profile_wizard] %>
<% @path = wizard_path %>
<% @is_remote = false %>
<% else %>
<% @path = home_path %>
<% @is_remote = true %>
<% end %>
<%= form_for(@home, :url => @path, :method => :put, :remote => @is_remote) do |form| %>
Home Update Func
def update
respond_to do |format|
if @home.update(home_params)
format.js {render :js => "window.location.href='"+user_path(@home.user)+"'", notice: 'Your profile was successfully updated.' }
else
format.js {}
end
end
end
User Model:
has_one :home, dependent: :destroy
Home Model:
belongs_to :user
现在的问题:我单击更新,这是“参数”放入控制台的内容。
UPDATE "homes" SET "is_interested" = $1, "updated_at" = $2 WHERE "homes"."id" = $3 [["is_interested", true], ["updated_at", "2019-09-05 14:12:27.352475"], ["id", 2]]
应该在homes.user_id = $3
答案 0 :(得分:0)
我解决它的方法是在更新功能中
if current_user.home.update(home_params)
代替
@home.update(home_params)