【问题标题】:form not saving to the database表单不保存到数据库
【发布时间】:2013-06-28 11:32:17
【问题描述】:

我正在尝试保存插入到表单中的数据,当我按下保存并继续时,它再次进入“新”,我尝试更改控制器中的错误操作并将其重定向到任何页面应用程序,但它根本不响应错误操作,并且浏览器中的 url 更改为:

0.0.0.0:3000/students/15/student_previous_data/new?utf8=%E2%9C%93&student_previous_data[student_id]=15&student_previous_data[institution]=institution&student_previous_data[course]=course&student_previous_data[year]=2010&student_previous_data[total_mark]=60&commit=Save+%26+Proceed

对于年份属性,我使用的是 bootstrap-datepicker ,并且我使用了这个 JQuery 代码

jQuery ->
  $(".year").datepicker( { format: "yyyy", viewMode: "years", minViewMode: "years", autoclose: true });

这就是我的控制器中的创建操作

  def create
    @student_previous_data = StudentPreviousData.new(params[:student_previous_data])
    if @student_previous_data.save
      flash[:success] = 'Record Saved Successfully.'
      redirect_to '/user/dashboard'
    else
      flash.now[:error] = 'An error occurred please try again!'
      render 'new'
    end
  end

student_previous_data/new.html.erb

<h1>Admission</h1>
<h4>Step 3 - Previous details</h4>

<div class="row-fluid">
  <div class="span5 offset1 hero-unit">
    <%= bootstrap_form_for(@student_previous_data, :url => new_student_student_previous_datum_path(@student),
                           html: { class: 'form-horizontal' }, method: :get) do |f| %>
        <% if @student_previous_data.errors.any? %>
            <div id="error_explanation">
              <div class="alert alert-error">
                The form contains <%= pluralize(@student_previous_data.errors.count, 'error') %>
              </div>
              <ul>
                <% @student_previous_data.errors.full_messages.each do |msg| %>
                    <li>* <%= msg %></li>
                <% end %>
              </ul>
            </div>
        <% end %>


            <fieldset>
              <div class="field">
                <%= f.hidden_field :student_id, value: @student.id %>

              </div>

              <h4>Previous Educational Details</h4>
              <div class="field">
                <%= f.text_field :institution, label: 'Institution Name'%>
              </div>
              <div class="field">
                <%= f.text_field :course, label: 'Course'%>
              </div>
              <div class="field">
                <%= f.text_field :year , class: 'year', label: 'Year' %>
              </div>
              <div class="field">
                <%= f.text_field :total_mark, label: 'Total Mark'%>
              </div>
              <div class="actions"><%= f.submit 'Save & Proceed', class: 'btn btn-mini btn-primary' %></div>
            </fieldset>
        <% end %>
      </div>

运行Rake routes 命令后:

        student_previous_data_index GET    /student_previous_data/index(.:format)                         student_previous_data#index
         student_previous_data_show GET    /student_previous_data/show(.:format)                          student_previous_data#show
          student_previous_data_new GET    /student_previous_data/new(.:format)                           student_previous_data#new
         student_previous_data_edit GET    /student_previous_data/edit(.:format)                          student_previous_data#edit
                    guardians_index GET    /guardians/index(.:format)                                     guardians#index
                     guardians_show GET    /guardians/show(.:format)                                      guardians#show
                     guardians_edit GET    /guardians/edit(.:format)                                      guardians#edit
                     students_index GET    /students/index(.:format)                                      students#index
                      students_show GET    /students/show(.:format)                                       students#show
                      students_edit GET    /students/edit(.:format)                                       students#edit
                   article_comments GET    /articles/:article_id/comments(.:format)                       comments#index
                                    POST   /articles/:article_id/comments(.:format)                       comments#create
                new_article_comment GET    /articles/:article_id/comments/new(.:format)                   comments#new
               edit_article_comment GET    /articles/:article_id/comments/:id/edit(.:format)              comments#edit
                    article_comment GET    /articles/:article_id/comments/:id(.:format)                   comments#show
                                    PUT    /articles/:article_id/comments/:id(.:format)                   comments#update
                                    DELETE /articles/:article_id/comments/:id(.:format)                   comments#destroy
                           articles GET    /articles(.:format)                                            articles#index
                                    POST   /articles(.:format)                                            articles#create
                        new_article GET    /articles/new(.:format)                                        articles#new
                       edit_article GET    /articles/:id/edit(.:format)                                   articles#edit
                            article GET    /articles/:id(.:format)                                        articles#show
                                    PUT    /articles/:id(.:format)                                        articles#update
                                    DELETE /articles/:id(.:format)                                        articles#destroy
                              users GET    /users(.:format)                                               users#index
                                    POST   /users(.:format)                                               users#create
                           new_user GET    /users/new(.:format)                                           users#new
                          edit_user GET    /users/:id/edit(.:format)                                      users#edit
                               user GET    /users/:id(.:format)                                           users#show
                                    PUT    /users/:id(.:format)                                           users#update
                                    DELETE /users/:id(.:format)                                           users#destroy
      student_student_previous_data GET    /students/:student_id/student_previous_data(.:format)          student_previous_data#index
                                    POST   /students/:student_id/student_previous_data(.:format)          student_previous_data#create
 new_student_student_previous_datum GET    /students/:student_id/student_previous_data/new(.:format)      student_previous_data#new
edit_student_student_previous_datum GET    /students/:student_id/student_previous_data/:id/edit(.:format) student_previous_data#edit
     student_student_previous_datum GET    /students/:student_id/student_previous_data/:id(.:format)      student_previous_data#show
                                    PUT    /students/:student_id/student_previous_data/:id(.:format)      student_previous_data#update
                                    DELETE /students/:student_id/student_previous_data/:id(.:format)      student_previous_data#destroy
                  student_guardians GET    /students/:student_id/guardians(.:format)                      guardians#index
                                    POST   /students/:student_id/guardians(.:format)                      guardians#create
               new_student_guardian GET    /students/:student_id/guardians/new(.:format)                  guardians#new
              edit_student_guardian GET    /students/:student_id/guardians/:id/edit(.:format)             guardians#edit
                   student_guardian GET    /students/:student_id/guardians/:id(.:format)                  guardians#show
                                    PUT    /students/:student_id/guardians/:id(.:format)                  guardians#update
                                    DELETE /students/:student_id/guardians/:id(.:format)                  guardians#destroy
                           students GET    /students(.:format)                                            students#index
                                    POST   /students(.:format)                                            students#create
                        new_student GET    /students/new(.:format)                                        students#new
                       edit_student GET    /students/:id/edit(.:format)                                   students#edit
                            student GET    /students/:id(.:format)                                        students#show
                                    PUT    /students/:id(.:format)                                        students#update
                                    DELETE /students/:id(.:format)                                        students#destroy
                           sessions GET    /sessions(.:format)                                            sessions#index
                                    POST   /sessions(.:format)                                            sessions#create
                        new_session GET    /sessions/new(.:format)                                        sessions#new
                       edit_session GET    /sessions/:id/edit(.:format)                                   sessions#edit
                            session GET    /sessions/:id(.:format)                                        sessions#show
                                    PUT    /sessions/:id(.:format)                                        sessions#update
                                    DELETE /sessions/:id(.:format)                                        sessions#destroy
                     user_dashboard GET    /user/dashboard(.:format)                                      static_pages#home
                 student_admission1 GET    /student/admission1(.:format)                                  students#new
                 student_admission2 GET    /student/admission2(.:format)                                  guardians#new
                               root        /                                                              sessions#new

如何使表单变为真实的以保存在数据库中?

【问题讨论】:

  • 保存/继续逻辑的视图代码是什么样的?
  • 问题已更新,我添加了 new.html.erb
  • 您的网址错误。运行 rake 路线并找到正确的路线
  • rake routes 添加到问题中,我该怎么办??
  • 从 Rails 指南开始。看起来你不知道自己在做什么。

标签: ruby-on-rails ruby-on-rails-3 forms datepicker


【解决方案1】:

您的表单网址不正确。您的网址应如下所示:

:url => student_student_previous_data_path(@student)

您当前的网址

:url => new_student_student_previous_datum_path(@student)

总是会将您直接重定向到您的新操作。你永远不会通过 create 动作。

【讨论】:

  • 我以前试过这个,现在又试了一次,以确保它不起作用,因为它在重定向到新表单之前引发了一个错误,这是我以前的错误,如果我会再次发生使用您的解决方案stackoverflow.com/questions/17353929/…
  • 你还有method: :get。您需要发布。
  • 老兄,我试过了,但它引发了,路由错误没有路由匹配 {:action=>"show", :controller=>"student_previous_data", :student_id=>#
【解决方案2】:

您正在使用GET 方法,请使用POST 方法。

并在网址中使用student_student_previous_data_path(@student)

【讨论】:

  • 不工作会引发路由错误 没有路由匹配 {:action=>"show", :controller=>"student_previous_data", :student_id=>#
  • 它不应该访问 POST 方法的显示操作。你能粘贴你的routes.rb 文件吗?
【解决方案3】:

将您的 form_for 更改为

<%= bootstrap_form_for(@student_previous_data, :url => student_student_previous_data_path(@student),
                       html: { class: 'form-horizontal' }, method: :post) do |f| %>

来自

<%= bootstrap_form_for(@student_previous_data, :url => new_student_student_previous_datum_path(@student),
                       html: { class: 'form-horizontal' }, method: :get) do |f| %>

它会起作用的。

【讨论】:

    【解决方案4】:

    名称本身有问题,控制器应该接受名称为singular,但表单接受名称为plural,我创建了一个新的controllermodel,名称为@987654325 @ 在控制器中应该是 >> redirect_to new_student_previous_detail_path(@student) 并且在表单中应该是 student_previous_details_path(@student)

    【讨论】:

      猜你喜欢
      • 2016-05-18
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      相关资源
      最近更新 更多