在我的Rails 3.2的項目,我有一個表格在app/views/sites/
的Rails:訪問參數鑑於表單提交後
<%= form_for(@site) do |site_form| %>
...
<div class="field">
<%= site_form.label :hash_name %><br />
<%= site_form.text_field :hash_name %>
</div>
...
<div class="actions">
<%= site_form.submit %>
</div>
<% end %>
然後create
函數創建new.html.erb
一個新的網站在sites_controller.rb
def create
@site = Site.new(params[:site])
respond_to do |format|
if @site.save
format.html { redirect_to @site }
else
format.html { render action: "new" }
end
end
end
然後在用戶提交後,我想顯示用戶剛剛提交的hash_name
,在show.html.erb
You just put in <%= params[:hash_name] %>.
但訪問params[:hash_name]
不起作用。我怎樣才能訪問它?
好吧,直接從'@ site'工作。 無論如何,如何通過'hash_name'作爲參數?我嘗試了'redirect_to @site,:hash_name => params [:hash_name]',然後訪問'params [:hash_name]會產生空的。 –
不需要將它作爲參數傳遞,但要做到這一點,您需要將其添加到生成的路線中,例如。 'redirect_to site_path(@site,hash_name:hash_name)'(或者你的路由被調用)。 – sevenseacat
好的,謝謝! –