Skip to content

Instantly share code, notes, and snippets.

@Baael
Created February 27, 2009 10:48
Show Gist options
  • Save Baael/71409 to your computer and use it in GitHub Desktop.
Save Baael/71409 to your computer and use it in GitHub Desktop.
# teraz uzyjemy form_for bo mamy obiekt
<div><%= error_messages_for 'news' %></div>
<% form_for(@news, :url => {:controller =>'news', :action =>'create'}) do |f| %>
Temat:<%= f.text_field :subject %><br />
Skrót:<%= f.text_field :body_short %><br />
News:<%= f.text_field :body %><br />
Data publikacji:<%= f.text_field :public_date%><br />
## wyjeb ta linijke !
##Publikowany przez:<%= f.text_field :user, session[:login] %><br />
<%= f.submit('dadaj newsa', :confirm => 'chcesz je dodać?') %>
<% end %>
<%= link_to 'wróć', :controller => 'admin' %>
#rozwijasz teraz metode create:
def create
#RELACJA Z USEREM
user=User.find_by_login(session[:login]) || User.first
#tworzysz newsa w oparciu o strukture jaką dostałeś z parametrów formularza
@news=user.news.new params[:news]
#sprawdzasz czy sie zapisuje (czyli czy przechodzi walidacje)
if @news.save
#jesli tak to zajebiscie i przekierowujemy sie do listy
redirect_to :action => :index
else
#jesli nie to przejebane i musimy jeszcze raz go wypelnic
#czyli wyswietlamy (ale nie przekierowywujemy!) formularz
render :action => :new
#renderujemy szablon akcji "new"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment