Ruby on rails RubyonRails-使用submit获得不同的页面
我有个小问题。我正在做一个小项目,我被一件相当简单的事情困住了,但无法通过它。我有一个表单,提交后,它会将我重定向到“show”,但我想更改它会将我重定向到“show15”。我是rails的新手,所以这可能是一个有点愚蠢的问题,但这确实是一个非常有用的帮助。提前谢谢 这是我的表格:Ruby on rails RubyonRails-使用submit获得不同的页面,ruby-on-rails,Ruby On Rails,我有个小问题。我正在做一个小项目,我被一件相当简单的事情困住了,但无法通过它。我有一个表单,提交后,它会将我重定向到“show”,但我想更改它会将我重定向到“show15”。我是rails的新手,所以这可能是一个有点愚蠢的问题,但这确实是一个非常有用的帮助。提前谢谢 这是我的表格: <%= form_for(@patient) do |f| %> <% if @patient.errors.any? %> <div id="error_explana
<%= form_for(@patient) do |f| %>
<% if @patient.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@patient.errors.count, "error") %> prohibited this patient from being saved:</h2>
<ul>
<% @patient.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :imie %><br />
<%= f.text_field :imie %>
</div>
<div class="field">
<%= f.label :nazwisko %><br />
<%= f.text_field :nazwisko %>
</div>
<div class="field">
<%= f.label :adres %><br />
<%= f.text_field :adres %>
</div>
<div class="field">
<%= f.label :pesel %><br />
<%= f.text_field :pesel %>
</div>
<div class="field">
<%= f.label :telefon %><br />
<%= f.text_field :telefon %>
</div>
<div class="field">
<%= f.label :doktor %><br />
<%= collection_select(:imie, :imie, Doctor.all, :id, :full_name) %>
</div>
<div class="actions">
<%= submit_tag "Edytuj", class: "show15", value: 'Edytuj' %>
</div>
<% end %>
禁止保存此患者:
假设您的表单将带您进入患者控制器内的创建操作。当患者保存在数据库中时,只需将其重定向到创建操作中的自定义操作
def create
@patient = Patient.new patient_params
if @patient.save
redirect_to your_path_of_show15
else
render new
end
end
我假设在您的创建操作中,您会有类似于重定向到@patient的内容,rails会通过它将您带到新创建的patient的显示操作。有关rails中路由如何工作的更多信息,请参考如果您查看您的
患者\控制器(我根据您的@patient
变量的名称猜测其名称),您可能有如下情况:
def create
@patient = Patient.new(patients_params)
respond_to do |format|
if @patient.save
format.html { redirect_to @patient, notice: 'Patient was successfully created.' }
format.json { render json: @patient, status: :created, location: @patient }
else
format.html { render action: "new" }
format.json { render json: @patient.errors, status: :unprocessable_entity }
end
end
end
resources :patients do
collection do
get :show15
...
end
end
Rails使用一种方法。这一行具体如下:
format.html { redirect_to @patient, notice: 'Patient was successfully created.' }
是什么导致show
页面“自动”加载的。你可以通过几种方式改变这种行为。一种方法是直接在方法调用中插入url,如下所示:
format.html { redirect_to "patients/show15" ... }
或者,根据您如何设置routes.rb
,如果您这样做:
def create
@patient = Patient.new(patients_params)
respond_to do |format|
if @patient.save
format.html { redirect_to @patient, notice: 'Patient was successfully created.' }
format.json { render json: @patient, status: :created, location: @patient }
else
format.html { render action: "new" }
format.json { render json: @patient.errors, status: :unprocessable_entity }
end
end
end
resources :patients do
collection do
get :show15
...
end
end
然后您可以这样做:
format.html { redirect_to show15_patients_path ... }
伟大的就这样!非常感谢你!谢谢它成功了:)