繁体   English   中英

Ruby on Rails-使用提交获取不同的页面

[英]Ruby on rails - using submit to get different page

我有点问题 我正在做一个小项目,我坚持做一些相当简单的事情,但是无法克服它。 我有一个表单,提交后将其重定向到查看“ show”的位置,但是我想更改它将其重定向到“ show15”的位置。 我对Rails很陌生,所以这可能是一个stupdi问题,但确实会有所帮助。 提前致谢。

那是我的形式:

<%= 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 %>

假设您的表格将带您进入患者控制器内部create动作 当患者保存在数据库中时,只需要将其重定向到 create操作中的自定义操作即可

def create
  @patient = Patient.new patient_params
  if @patient.save
    redirect_to your_path_of_show15
  else
    render new
  end
end

我假设在您的create动作中,您将有一个redirect_to @ Patient之类的东西, 通过它rails会将您带到新创建的Patient的show动作。 有关路线如何在导轨中工作的更多信息,请参考导轨导轨

如果你看看你的patients_controller (我猜的名字,根据你的名字@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

Rails使用RESTful路由方法。 此行专门:

format.html { redirect_to @patient, notice: 'Patient was successfully created.' }

是导致show页面“自动”加载的原因。 您可以通过两种方法更改此行为。 一种方法是直接将url直接插入方法调用中,如下所示:

format.html { redirect_to "patients/show15" ... }

或者,取决于您如何设置routes.rb ,如果您执行以下操作:

resources :patients do
    collection do
        get :show15
        ...
    end
end

然后您可以执行以下操作:

format.html { redirect_to show15_patients_path ... }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM