Skip to content

Instantly share code, notes, and snippets.

@gmassanek
Created March 9, 2011 18:19
Show Gist options
  • Save gmassanek/862676 to your computer and use it in GitHub Desktop.
Save gmassanek/862676 to your computer and use it in GitHub Desktop.
class PagesController < ApplicationController
def home
@title = "Home"
puts flash[:notice]
@subscription = Subscription.new
@announcements = Announcement.find(:all, :order => 'updated_at DESC')
end
end
home.html.erb contains:
<%= form_for(@subscription) do |f| %>
<% if @subscription.errors.any? %>
<div id="error_explanation">
Please fix the following <%= pluralize(@subscription.errors.count, "error") %>:
<ul>
<% @subscription.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= f.label :name %><br />
<%= f.text_field :name, :id => "newsletter_email"%>
</div>
<div>
<%= f.label :email %><br />
<%= f.text_field :email, :id => "newsletter_email" %>
</div>
<div>
<%= f.submit %>
</div>
<% end %>
class SubscriptionsController < ApplicationController
def create
session[:subscription] = nil
@subscription = Subscription.new(params[:subscription])
if @subscription.save
puts "successful save"
redirect_to home_path
else
puts "unsuccessful save"
render root_path
end
#end
end
end
This all works great other than that @announcements from pages#home is nil the second time around...
@gmassanek
Copy link
Author

So I didn't figure it out completely. What I ended up doing was adding another action to the pages controller called save_subscription that just does an @subscription load and save. Doing it this way may not be perfectly RESTful, but it preserved my local variables and got me what I needed. Still not sure if it's the best way to do it but it worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment