Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Access parameters in view after form submission

In my Rails 3.2 project, I have a form to create a new site in new.html.erb in app/views/sites/

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

Then the create function in 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

Then after the user submits, I want to show the hash_name that the user just submitted, in show.html.erb

You just put in <%= params[:hash_name] %>.

But accessing params[:hash_name] doesn't work. How can I access it?

like image 688
Paul S. Avatar asked Oct 11 '12 01:10

Paul S.


2 Answers

You're redirecting to a different action - this will reset all the parameters you passed to the create action.

Either pass hash_name as a parameter like the other answer suggests, or just fetch it straight from the @site object.

like image 62
sevenseacat Avatar answered Sep 22 '22 02:09

sevenseacat


Just append them to the options:

redirect_to @site, :hash_name => params[:hash_name]

like image 30
Carlos Pereira Avatar answered Sep 19 '22 02:09

Carlos Pereira