def update
if @note.update_attributes(note_params)
redirect_to :back, notice: "Note was updated."
else
render :edit
end
end
有沒有辦法重新導向兩次?如何redirect_to(:back)兩次?
def update
if @note.update_attributes(note_params)
redirect_to :back, notice: "Note was updated."
else
render :edit
end
end
有沒有辦法重新導向兩次?如何redirect_to(:back)兩次?
在這裏你去: 這就是鏈接編輯得好:
<p id="notice"><%= notice %></p>
<% url = "#{request.protocol}#{request.host_with_port}#{request.fullpath}" %>
<%= link_to 'Create New Page and Return Here', edit_page_path(1, :url => Base64.encode64(url)) %>
<br>
提交後,您的網址將是這樣的: http://localhost:3000/pages/1/edit?url=aHR0cDovL2xvY2FsaG9zdDozMDAwL2R1bW1pZXM%3D%0A
在編輯表單: 我把它叫做pages/_form.html.erb,將URL作爲隱藏參數傳遞。
<%= form_for(@page) do |f| %>
<% if @page.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@page.errors.count, "error") %> prohibited this page from being saved:</h2>
<ul>
<% @page.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :permalink %><br>
<%= f.text_field :permalink %>
</div>
<%= hidden_field_tag :url, params[:url].to_s %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
在控制器,你有更新的方法,在這種情況下pages_controller.rb,只需Base64
回來,並把用戶重定向:
def update
redirection = nil
if params[:url].present?
redirection = Base64.decode64(params[:url].to_s)
end
if @page.update(page_params)
if redirection.present?
path = redirection
else
path = @page
end
redirect_to path, notice: 'All Done.'
else
render :edit
end
end
現在,用戶更新的形式和重定向到首秀或索引頁面或她來自的任何頁面。
希望得到這個幫助。 PS:你可能想清理它一下,並從控制器傳遞URL,並對其進行一些檢查。所以你不要在視圖級別定義任何變量。在上面的代碼中,我試圖解決這個問題,而不是真正的設計模式:)
不用擔心隊友。很高興我能幫上忙。乾杯 –
你怎麼知道它們來自兩個點擊前? – Makoto
任何有用的[這些答案](http://stackoverflow.com/questions/2139996/ruby-on-rails-redirect-toback)? – Kimball
他們來自四個演出頁面@Makoto之一,因爲音符是多態的,所以當他們點擊編輯他們的音符然後點擊更新他們被重定向迴音符編輯表單,但我希望他們重新回到顯示頁面,如果可能的話: ) –