我正在尝试保存插入到表单中的数据,当我按下save&继续它再次转到'新'并且我试图改变控制器中的错误操作并将其重定向到应用程序的任何页面但它根本不响应错误操作,并且浏览器中的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
对于year属性,我使用的是bootstrap-datepicker,我使用这个JQuery Code
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
如何让表单转到true以保存在数据库中?
答案 0 :(得分:1)
您的表单网址不正确。您的网址应如下所示:
:url => student_student_previous_data_path(@student)
您当前的网址
:url => new_student_student_previous_datum_path(@student)
始终会将您直接重定向到您的新操作。您永远不会传递创建操作。
答案 1 :(得分:1)
您正在使用GET
方法,请使用POST
方法。
在网址中使用student_student_previous_data_path(@student)
答案 2 :(得分: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| %>
它会起作用。
答案 3 :(得分:1)
这是名称本身的一个问题,控制器应该接受名称为singular
,但表单接受名称为plural
,我创建了一个新的controller
和{{ 1}}在控制器中名称为model
,它应该是&gt;&gt; 'previous_detail'
并且表单应为redirect_to new_student_previous_detail_path(@student)