Created
March 9, 2011 18:19
-
-
Save gmassanek/862676 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... |
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
Looks like if you're having a problem with @title being nil, it's possibly because you're rendering the root_path instead of directly back to the home action on the pages controller. You ever figure this one out?