1

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

我如何使表格真正保存在数据库中?

4

4 回答 4

1

将您的 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| %>

它会起作用。

于 2013-06-29T08:47:45.947 回答
1

这是名称本身的问题,控制器应该接受名称,singular但表单接受名称plural,我已经创建了一个新名称,controller并且控制器中model的名称'previous_detail'应该是>>redirect_to new_student_previous_detail_path(@student)并且表单应该是student_previous_details_path(@student)

于 2013-06-29T10:16:10.097 回答
1

你在使用GET方法,使用POST方法。

student_student_previous_data_path(@student)在 url 中使用

于 2013-06-28T16:02:21.137 回答
1

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

:url => student_student_previous_data_path(@student)

您当前的网址

:url => new_student_student_previous_datum_path(@student)

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

于 2013-06-28T13:21:48.423 回答