gpt4 book ai didi

mysql - 我无法将提交保存到数据库

转载 作者:行者123 更新时间:2023-11-30 00:10:22 24 4
gpt4 key购买 nike

我没有错误,但是当我单击“确定”(提交按钮)时什么也没有发生,我的数据库根本没有改变,我正在做我的应用程序,就像我按一个按钮一样,然后所有接下来的页面都已经加载,我使用ajax和jquery在它们之间导航,并在其中一个页面中调用我的表单。可能是因为这个?当我点击“确定”(提交按钮)时,它在控制台上确实没有反应:S

我的部分表单: _impediments_risks.html.erb

 <div class="item-list">
<%@solution = Solution.new(params[:solution])%>
<% for i in (0..session[:project_impediments].size-1) %>
<div class="item">
<% impediment = session[:project_impediments][i][1] %>
<div class="title">
<span class="indicator"><%= impediment.priority.name %></span>
<div class="description"><%= impediment.subject %></div>
</div>
<div class="content">
<span class="title"><%= I18n.t 'text.solution' %></span>
<ul>
<% sol = ApplicationHelper.apply_filter(ApplicationHelper.Hash_DB(Solution.where('user_id IS NULL or user_id=?',session[:user])), ApplicationHelper.stem_phrase(impediment.subject.downcase)) %>
<% sol.each do |s| %>
<li><%= s %></li>
<% end %>
<li><b>NEW SOLUTION</b></li>
<li>
<%= form_for(@solution) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :text %>
<%= f.text_field :text %>
<%= f.label :keywords %>
<%= f.text_field :keywords %>
<%= f.submit "Ok", class: "btn btn-large btn-primary" %>
<% end %>
</li>
</ul>
</div>
</div>
<% end %>
</div>

我的 Controller 解决方案_controller.rb

class SolutionsController < ApplicationController

def new
@solution = Solution.new
end

def create
@solution = Solution.new(solution_params)
if @solution.save
#flash.now[:success] = "New Solution created!"
else
#Something
end
end

private

def solution_params
params.require(:solution).permit(:text, :keywords)
end
end

我的模型:解决方案.rb

class Solution < ActiveRecord::Base
attr_accessible :text, :keywords, :contar , :user_id

before_save { |solution| solution.keywords = solution.keywords.downcase }

default_scope -> { order('contar DESC') }
validates :text, presence: true, uniqueness: { case_sensitive: false }

VALID_KEYS_REGEX = /\A([a-zA-Z]{3,})+(&|\|[a-zA-Z]{3,}+)*\z/i
validates :keywords, presence:true, format: { with: VALID_KEYS_REGEX }
end

最佳答案

您的解决方案对象似乎未保存,可能是因为它验证失败。通常,您会在表单中收到一些针对失败的 @solution 对象的错误消息,但您永远不会看到这些消息,因为您每次都会覆盖 @solution。

应该发生的是这样的:

a) 制作一个新的 - 新操作创建一个新的 @solution 对象并呈现一个使用 form_for @solution 的页面

b) 保存一个新的 - create 操作使用 params[:solution] 创建一个新的 @solution 对象。如果保存失败,它会渲染出使用 form_for @solution 的同一页面。 。此页面通常会显示@solution 的错误。

当 @Pavan 说“尝试像这样给出 <%= form_for(@solution, ... ”时,他假设以这种形式呈现页面的每个操作都将定义 @solution 。这是标准的铁路方式。

此外,form_for 将自动发送到创建或更新操作,具体取决于它是否是新对象。因此,您不应该像使用 url 选项那样强制它创建。

我会将您的代码更改为:

#controller
def create
@solution = Solution.new(params[:solution])
if @solution.save
flash.now[:success] = "New Solution created!"
#redirect wherever you go after the object is saved
else
render :template => <your page with the form partial>
end
end

def update
@solution = Solution.find(params[:id])
@solution.attributes = params[:solution]
if @solution.save
flash.now[:success] = "Solution saved!"
#redirect wherever you go after the object is saved
else
render :template => <your page with the form partial>
end
end

#view
...
<%= form_for(@solution) do |f| %>
...

关于mysql - 我无法将提交保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24117955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com