Permalink
Browse files

blog refactored

  • Loading branch information...
1 parent b2d4e47 commit 34ed6a2ad57684a966781f91459aa7f726ba26f4 @avarteq avarteq committed May 27, 2010
@@ -1,4 +1,6 @@
class PostsController < AuthenticatedController
+
+ before_filter :load_categories, :only => [:new, :edit]
# GET /posts
# GET /posts.xml
@@ -26,7 +28,7 @@ def show
# GET /posts/new.xml
def new
@post = Post.new
-
+
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @post }
@@ -83,4 +85,10 @@ def destroy
format.xml { head :ok }
end
end
+
+ protected
+
+ def load_categories
+ @categories = Category.all
+ end
end
@@ -1,3 +1,8 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
+ [:div, :ul].each do |method_name|
+ define_method method_name do |options, &block|
+ content_tag method_name, options, &block
+ end
+ end
end
@@ -3,11 +3,11 @@ def comment_form_id(post)
"comment_form_id_#{post.id}"
end
- def post_comments_id(post_id)
- "post_ul_id_#{post_id}"
+ def post_comments_id(post_id, label = "")
+ "post_ul_id_#{post_id}_#{label}"
end
- def link_to_write_comment(post)
- link_to_function "Kommentar schreiben", "$('#{comment_form_id(post)}').show();"
+ def link_to_show_comments(post)
+ link_to_function "Kommentare anzeigen", "$('#{post_comments_id(post.id, :div)}').show();"
end
end
@@ -1,14 +1,16 @@
-<%= link_to_write_comment(post) %>
-<br/>
-<% content_tag :div, :id => comment_form_id(post), :style => 'display:none' do %>
+<br/>
+<% content_tag :div, :id => comment_form_id(post) do %>
<% remote_form_for Comment.new(:post => post) do |f| %>
<%= f.text_area "body", :rows => 2 %>
<%= f.hidden_field "post_id" %>
<br/>
<%= f.submit %>
<% end %>
+
+ <br/>
+
<% end %>
@@ -2,10 +2,17 @@
<p>
<%= post.body %>
</p>
-<% content_tag :ul, :id => post_comments_id(post.id) do %>
- <% post.comments.each do |comment| %>
- <%= render :partial => 'comment', :locals => { :comment => comment } %>
+
+<% div(:style =>"display:none;", :id => post_comments_id(post.id, :div)) do %>
+ <% ul :id => post_comments_id(post.id) do %>
+ <% post.comments.each do |comment| %>
+ <%= render :partial => 'comment', :locals => { :comment => comment } %>
+ <% end %>
<% end %>
+
+ <%= render :partial => 'comment_form', :locals => { :post => post } %>
<% end %>
-<%= render :partial => 'comment_form', :locals => { :post => post } %>
+<%= link_to_show_comments(post) %>
+
+
<hr/>
@@ -0,0 +1,14 @@
+Kategorien
+<br />
+
+<% @categories.each do |category| %>
+
+ <%= check_box_tag "post[category_ids][]", "#{category.id}", @post.categories.include?(category) %>
+ &nbsp;
+
+
+ <%= "#{category.title}" %>
+
+ <br />
+
+<% end %>
@@ -0,0 +1,21 @@
+<% form_for(@post) do |f| %>
+ <%= f.error_messages %>
+
+ <%= render :partial => 'categories' %>
+
+ <p>
+ <%= f.label :title %><br />
+ <%= f.text_field :title %>
+ </p>
+ <p>
+ <%= f.label :body %><br />
+ <%= f.text_area :body %>
+ </p>
+ <p>
+ <%= f.label :published %><br />
+ <%= f.check_box :published %>
+ </p>
+ <p>
+ <%= f.submit 'Save' %>
+ </p>
+<% end %>
@@ -1,24 +1,6 @@
<h1>Editing post</h1>
-<% form_for(@post) do |f| %>
- <%= f.error_messages %>
-
- <p>
- <%= f.label :title %><br />
- <%= f.text_field :title %>
- </p>
- <p>
- <%= f.label :body %><br />
- <%= f.text_area :body %>
- </p>
- <p>
- <%= f.label :published %><br />
- <%= f.check_box :published %>
- </p>
- <p>
- <%= f.submit 'Update' %>
- </p>
-<% end %>
+<%= render :partial => 'form' %>
<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>
@@ -1,23 +1,6 @@
<h1>New post</h1>
-<% form_for(@post) do |f| %>
- <%= f.error_messages %>
+<%= render :partial => 'form' %>
- <p>
- <%= f.label :title %><br />
- <%= f.text_field :title %>
- </p>
- <p>
- <%= f.label :body %><br />
- <%= f.text_area :body %>
- </p>
- <p>
- <%= f.label :published %><br />
- <%= f.check_box :published %>
- </p>
- <p>
- <%= f.submit 'Create' %>
- </p>
-<% end %>
<%= link_to 'Back', posts_path %>

0 comments on commit 34ed6a2

Please sign in to comment.