【问题标题】:My form parameters are not saving/inserting to my database, even though the form is submitting the paramters我的表单参数没有保存/插入到我的数据库,即使表单正在提交参数
【发布时间】:2015-04-16 19:34:38
【问题描述】:

这是我的表格:

    <form class="form-horizontal">
        <%= simple_form_for SkyscraperApplications.create, :url=>"/about/skyscraper/submit", :method=>"post" do |f| %>
      <div class="control-group">
        <label class="control-label" for="name"><%=t ".company_name"%></label>
        <div class="controls">
          <div><%= f.input :name,  :label=>false %></div>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="employees"><%=t ".no_of_employees"%></label>
        <div class="controls">
          <div><%= f.input :employee_count, :label=>false %></div>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="employees"><%=t ".usecase"%></label>
        <div class="controls">
          <%= f.select :usecase, options_for_select([["Select One", ""], "CAD/Render", "Video Production/Editing", "Music Production", "Game Design", "Email/Web", "Other"])%>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="Administrator"><%=t ".admin_email"%></label>
        <div class="controls">
          <div><%= f.input :email_admin, :label=>false %></div>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="Administrator"><%=t ".what_you_do"%></label>
        <div class="controls">
        <div><%= f.input :what_you_do, :label=>false, :as => :text, :input_html => { :rows => 4 }%></div>
      </div>
      </div>
      <div class="control-group">
        <div class="controls">
          <div class="test-drive-submit"><%= f.button :submit, t(".submit")%></div>
        </div>
      </div>
    </form>

这是我的控制器方法:

def create
if @skyscraper=SkyscraperApplications.new(:email_admin => params[:skyscraper_applications][:email_admin],:employee_count=>params[:skyscraper_applications][:employee_count],:name=>params[:skyscraper_applications][:name],:usecase=>params[:skyscraper_applications][:usecase],:what_you_do=>params[:skyscraper_applications][:what_you_do])
 @skyscraper.save
    redirect_to "/about/skyscraper", :notice => t("skyscraper_saved")

else
 redirect_to "/about/skyscraper", :notice => t("skyscraper_failed")

end 

这是我的控制台:

Processing by AboutController#skyscraper as HTML
  Parameters: {"utf8"=>"✓",     "authenticity_token"=>"fpIBeQKOqnP1As118fWwG5/LXdyT3QL5dLBYRDgkGNq8PfwldrvC9QrsZnJ+pX9+mvhCN3N9yW9p2q7fdhDdhg==", "skyscraper_applications"=>{"name"=>"example", "employee_count"=>"5", "usecase"=>"CAD/Render", "email_admin"=>"email@email.com", "what_you_do"=>"example"}, "commit"=>"Submit"}

  SkyRoutine Load (1.0ms)  SELECT  "sky_routines".* FROM "sky_routines"  ORDER BY "sky_routines"."id" ASC LIMIT 1

   (0.1ms)  begin transaction
  SQL (0.8ms)  INSERT INTO "skyscraper_applications" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2015-04-16 19:29:16.070954"], ["updated_at", "2015-04-16 19:29:16.070954"]]
   (2.1ms)  commit transaction
  Rendered about/skyscraper.html.erb within layouts/application (23.6ms)
  Rendered layouts/_navigation.html.erb (1.3ms)
  Rendered layouts/_messages.html.erb (0.2ms)
  Rendered layouts/_footer.html.erb (0.8ms)
Completed 200 OK in 90ms (Views: 82.2ms | ActiveRecord: 4.0ms)

如您所见,我提交了表单,它会获取参数但不会插入到我的数据库中并按我想要的方式保存。我做错了什么?!

【问题讨论】:

  • 可能是模型中的某些东西。能否请您发布SkyscraperApplications 模型的代码?
  • class SkyscraperApplications &lt; ActiveRecord::Base attr_accessor :email_admin, :employee_count, :name, :usecase, :what_you_do end 谢谢!!
  • 问题在于 attr_accessor 行。我刚刚发布了一个答案...
  • SkyscraperApplications.create 在你的表单对象中有什么意义?为什么不遵循 rails crud 约定?手动复杂的事情只会让你更糟
  • 它不是在调用我的控制器方法吗?你建议我在那里做些什么改变?

标签: html ruby-on-rails forms activerecord


【解决方案1】:

问题是您已经为要保存的属性定义了attr_accessors。 attr_accessor 定义了 setter 和 getter 方法,它们覆盖了任何同名的属性。删除 attr_accessors 后,Rails 应将值保存到数据库中。

【讨论】:

  • 感谢您的回复。我删除了 attr_accessors 但它仍在做同样的事情:/
【解决方案2】:

假设你使用的是 Rails 4.0.0 或更高版本,在进行批量赋值时需要使用强参数。您还需要在控制器中修复您的逻辑。它应该是这样的:

def create
  @skyscraper = SkyscraperApplications.new(sky_scraper_params)
  if @skyscraper.save
    redirect_to "/about/skyscraper", :notice => t("skyscraper_saved")
  else
    redirect_to "/about/skyscraper", :notice => t("skyscraper_failed")
end

private

def  sky_scraper_params
  params.require(:skyscraper_applications).permit(:name, :employee_count, :usecase, :email_admin, :what_you_do
end

【讨论】:

  • 感谢您,我添加了强大的参数并通过方法进行了调整,但我的控制台中仍然得到同样的结果。会不会是我的表格的问题?这让我很难过。
  • 您收到、保存或失败的通知是什么?
  • 好像你没有数据库中的属性或其他东西,发布架构。
  • 我没有收到通知。这是我的架构:create_table "skyscraper_applications", force: :cascade do |t| t.string "name", limit: 255 t.integer "employee_count" t.string "usecase", limit: 255 t.string "email_admin", limit: 255 t.string "what_you_do", limit: 255 t.datetime "created_at", null: false t.datetime "updated_at", null: false end
  • 您提交的操作有误,请发布您的路线
猜你喜欢
  • 1970-01-01
  • 2023-02-10
  • 2015-02-05
  • 1970-01-01
  • 2018-11-13
  • 2018-09-07
  • 1970-01-01
  • 2019-09-20
  • 1970-01-01
相关资源
最近更新 更多