Mysql 我无法在数据库中保存提交
我没有错误,但当我点击“确定”按钮提交时,什么都没有发生,我的数据库根本没有改变, 我在做我的应用程序,就像我按下一个按钮,接下来的所有页面都已经加载,我使用ajax和jquery在它们之间导航,我在其中一个页面中调整我的表单。可能是因为这个?当我点击ok submit(确定提交)按钮时,它实际上不会在控制台上做出反应:S 我的部分表格:_industries_risks.html.erbMysql 我无法在数据库中保存提交,mysql,ruby-on-rails,database,forms,save,Mysql,Ruby On Rails,Database,Forms,Save,我没有错误,但当我点击“确定”按钮提交时,什么都没有发生,我的数据库根本没有改变, 我在做我的应用程序,就像我按下一个按钮,接下来的所有页面都已经加载,我使用ajax和jquery在它们之间导航,我在其中一个页面中调整我的表单。可能是因为这个?当我点击ok submit(确定提交)按钮时,它实际上不会在控制台上做出反应:S 我的部分表格:_industries_risks.html.erb <div class="item-list"> <%@solution = Sol
<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>
我的控制器解决方案\u 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
我的模型:solution.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 应该发生的是: 制作一个新的 -新操作生成一个新的@solution对象,并呈现一个使用form_表示@solution的页面 b保存一个新的 -创建操作使用params[:solution]创建一个新的@solution对象。如果保存失败,它将显示使用表单_表示@solution的同一页面。此页面通常会显示@solution的错误
当@Pavan说,在提交表单时,试着像这样提供日志信息。也试着像这样提供{:action=>create,:controller=>solutions}do | f |%>如果你使用强参数,你不应该访问attr_,如果你试图对关键字进行降格,你应该使用self.keywords=keywords.downcasepavan当我这样做@solution时,它给了我一个针对nilclass的错误未定义方法。Baloo我在我的代码中注释出你所说的,它也不起作用。当我点击ok时,控制台上什么也没发生:S谢谢你们两个尝试太多帮助:在保存之前保存{self.email=email.downcase}对我有效。如果表单格式正确,请参阅表单的源html。页面是否指向某个url。您好,我尝试过这个,但它给我提供了一个错误ActionView::Template::error undefined方法'model_name'for NilClass:Class感谢您尝试提供帮助,这是因为在调用@solution的表单_时,尚未定义@solution。你正在调用的动作中定义了它吗?好的,我已经定义了它,错误消失了,但是当我按下ok时,它似乎没有反应。我在做我的应用程序,就像我按下一个按钮,接下来的所有页面都已经加载,我使用ajax和jquery在它们之间导航,我在其中一个页面中调整我的表单。可能是因为这个?当我点击ok submit(确定提交)按钮时,它在控制台上真的没有反应:SDo您的控制台中有任何javascript错误报告吗?我很抱歉刚才回答了您的问题,我知道。。我只有一个sintax错误,但在执行此步骤之前我已经有了它。。但一切都和以前一样,但我仍然存在无法保存到db的问题:
#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| %>
...