Skip to content

Instantly share code, notes, and snippets.

@workmad3
Last active August 29, 2015 14:00

Revisions

  1. workmad3 revised this gist Apr 25, 2014. 5 changed files with 4 additions and 25 deletions.
    2 changes: 1 addition & 1 deletion _form.html.haml
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@
    %div{class: 'field'}
    = f.text_field :name

    = f.fields_for @post do |p|
    = f.fields_for :posts, post do |p|
    = render :partial => "posts/form", :locals => { :f => p }

    %div{class: 'actions'}
    2 changes: 1 addition & 1 deletion edit.html.haml
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    %h1
    Edit topic
    = @topic.name
    = render 'form'
    = render 'form', post: @post
    2 changes: 1 addition & 1 deletion new.html.haml
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,3 @@
    %h1 New topic

    = render 'form'
    = render 'form', post: @topic.posts.first
    17 changes: 0 additions & 17 deletions rendered_html.html
    Original file line number Diff line number Diff line change
    @@ -1,17 +0,0 @@
    <h1>
    Edit topic
    mmmmmmmm
    </h1>
    <form accept-charset="UTF-8" action="/forums/ruby/topics/mmmmmmmm-932157ee-7b13-4af9-97af-7d99d668f73a" class="edit_topic" id="edit_topic_64" method="post"><div style="display:none"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="patch" /><input name="authenticity_token" type="hidden" value="UQL4ExZ0s+J/pZvwhXpJGK6ZxcCVuTVDsfbY+OCEkrY=" /></div> <label for="topic_name">Name</label>
    <div class='field'>
    <input id="topic_name" name="topic[name]" type="text" value="mmmmmmmm" />
    </div>
    <label for="topic_post_bodytext">Bodytext</label>
    <div class='field'>
    <textarea id="topic_post_bodytext" name="topic[post][bodytext]">
    mmmmmmmmmmmmmmmmmmmmmmmmmmmmm</textarea>
    </div>
    <div class='actions'>
    <input name="commit" type="submit" value="Update Topic" />
    </div>
    </form>
    6 changes: 1 addition & 5 deletions topics_controller.rb
    Original file line number Diff line number Diff line change
    @@ -18,9 +18,7 @@ def new
    end

    def edit
    @topic.posts.build
    @post = Post.find_by topic_id: @topic
    logger.debug(@post)
    @post = @topic.posts.first
    end

    def create
    @@ -38,10 +36,8 @@ def create
    end

    def update
    @topic = set_topic
    respond_to do |format|
    if @topic.update(topic_params)
    @topic.posts.build
    format.html { redirect_to forum_topic_path(@forum, @topic), notice: 'Topic was successfully updated.' }
    else
    format.html { render :edit }
  2. @drager drager revised this gist Apr 25, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions log-file.log
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    Started PATCH "/forums/ruby/topics/mmmmmmmm-932157ee-7b13-4af9-97af-7d99d668f73a" for 127.0.0.1 at 2014-04-25 11:10:56 +0200
    Processing by TopicsController#update as HTML
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"UQL4ExZ0s+J/pZvwhXpJGK6ZxcCVuTVDsfbY+OCEkrY=", "topic"=>{"name"=>"Lorem Ipsum", "post"=>{"bodytext"=>"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."}}, "commit"=>"Update Topic", "forum_id"=>"ruby", "id"=>"mmmmmmmm-932157ee-7b13-4af9-97af-7d99d668f73a"}
  3. @drager drager revised this gist Apr 25, 2014. 4 changed files with 16 additions and 17 deletions.
    6 changes: 2 additions & 4 deletions _form.html.haml
    Original file line number Diff line number Diff line change
    @@ -12,10 +12,8 @@
    %div{class: 'field'}
    = f.text_field :name

    = f.fields_for :post do |p|
    = p.label :bodytext
    %div{class: 'field'}
    = p.text_area :bodytext
    = f.fields_for @post do |p|
    = render :partial => "posts/form", :locals => { :f => p }

    %div{class: 'actions'}
    = f.submit
    3 changes: 3 additions & 0 deletions post_form.html.haml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    = f.label :bodytext
    %div{class: 'field'}
    = f.text_area :bodytext
    19 changes: 7 additions & 12 deletions rendered_html.html
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,15 @@
    <form accept-charset="UTF-8" action="/forums/ruby/topics/123123111" class="edit_topic" id="edit_topic_59" method="post"><div style="display:none"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="patch" /><input name="authenticity_token" type="hidden" value="UQL4ExZ0s+J/pZvwhXpJGK6ZxcCVuTVDsfbY+OCEkrY=" /></div> <div id='error_explanation'>
    <h2>
    1 error
    prohibited this topic from being saved:
    </h2>
    <li>
    Posts bodytext is too short (minimum is 20 characters)
    </li>
    </div>
    <label for="topic_name">Name</label>
    <h1>
    Edit topic
    mmmmmmmm
    </h1>
    <form accept-charset="UTF-8" action="/forums/ruby/topics/mmmmmmmm-932157ee-7b13-4af9-97af-7d99d668f73a" class="edit_topic" id="edit_topic_64" method="post"><div style="display:none"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="patch" /><input name="authenticity_token" type="hidden" value="UQL4ExZ0s+J/pZvwhXpJGK6ZxcCVuTVDsfbY+OCEkrY=" /></div> <label for="topic_name">Name</label>
    <div class='field'>
    <input id="topic_name" name="topic[name]" type="text" value="123123111555" />
    <input id="topic_name" name="topic[name]" type="text" value="mmmmmmmm" />
    </div>
    <label for="topic_post_bodytext">Bodytext</label>
    <div class='field'>
    <textarea id="topic_post_bodytext" name="topic[post][bodytext]">
    </textarea>
    mmmmmmmmmmmmmmmmmmmmmmmmmmmmm</textarea>
    </div>
    <div class='actions'>
    <input name="commit" type="submit" value="Update Topic" />
    5 changes: 4 additions & 1 deletion topics_controller.rb
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,9 @@ def new
    end

    def edit
    @topic.posts.build
    @post = Post.find_by topic_id: @topic
    logger.debug(@post)
    end

    def create
    @@ -36,9 +39,9 @@ def create

    def update
    @topic = set_topic
    @post = @topic.posts.build
    respond_to do |format|
    if @topic.update(topic_params)
    @topic.posts.build
    format.html { redirect_to forum_topic_path(@forum, @topic), notice: 'Topic was successfully updated.' }
    else
    format.html { render :edit }
  4. @drager drager created this gist Apr 25, 2014.
    24 changes: 24 additions & 0 deletions _form.html.haml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    = form_for([@forum, @topic]) do |f|
    - if @topic.errors.any?
    %div{id: 'error_explanation'}
    %h2
    = pluralize(@topic.errors.count, "error")
    prohibited this topic from being saved:
    - for message in @topic.errors.full_messages
    %li
    = message

    = f.label :name
    %div{class: 'field'}
    = f.text_field :name

    = f.fields_for :post do |p|
    = p.label :bodytext
    %div{class: 'field'}
    = p.text_area :bodytext

    %div{class: 'actions'}
    = f.submit

    - if @topic.new_record?
    asd
    4 changes: 4 additions & 0 deletions edit.html.haml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    %h1
    Edit topic
    = @topic.name
    = render 'form'
    3 changes: 3 additions & 0 deletions new.html.haml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    %h1 New topic

    = render 'form'
    22 changes: 22 additions & 0 deletions rendered_html.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <form accept-charset="UTF-8" action="/forums/ruby/topics/123123111" class="edit_topic" id="edit_topic_59" method="post"><div style="display:none"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="patch" /><input name="authenticity_token" type="hidden" value="UQL4ExZ0s+J/pZvwhXpJGK6ZxcCVuTVDsfbY+OCEkrY=" /></div> <div id='error_explanation'>
    <h2>
    1 error
    prohibited this topic from being saved:
    </h2>
    <li>
    Posts bodytext is too short (minimum is 20 characters)
    </li>
    </div>
    <label for="topic_name">Name</label>
    <div class='field'>
    <input id="topic_name" name="topic[name]" type="text" value="123123111555" />
    </div>
    <label for="topic_post_bodytext">Bodytext</label>
    <div class='field'>
    <textarea id="topic_post_bodytext" name="topic[post][bodytext]">
    </textarea>
    </div>
    <div class='actions'>
    <input name="commit" type="submit" value="Update Topic" />
    </div>
    </form>
    62 changes: 62 additions & 0 deletions topics_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    class TopicsController < ApplicationController
    before_filter :find_forum
    before_filter :authorize, only: [:new, :edit, :update]
    before_action :set_topic, only: [:show, :edit, :update]

    def index
    redirect_to forum_path(@forum)
    end

    def show
    @topic = Topic.friendly.find(set_topic)
    @posts = @topic.posts
    end

    def new
    @topic = Topic.new
    @topic.posts.build
    end

    def edit
    end

    def create
    @topic = @forum.topics.build(topic_params)
    @topic.author_id = current_user.id
    logger.debug(topic_params)

    respond_to do |format|
    if @topic.save
    format.html { redirect_to forum_topic_path(@forum, @topic), notice: 'Topic was successfully created.' }
    else
    format.html { render :new }
    end
    end
    end

    def update
    @topic = set_topic
    @post = @topic.posts.build
    respond_to do |format|
    if @topic.update(topic_params)
    format.html { redirect_to forum_topic_path(@forum, @topic), notice: 'Topic was successfully updated.' }
    else
    format.html { render :edit }
    end
    end
    end

    private
    def find_forum
    @forum = Forum.find(params[:forum_id])
    end

    def set_topic
    @topic = Topic.find(params[:id])
    end

    # Allow only the white list
    def topic_params
    params.require(:topic).permit(:name, author_id: current_user.id , posts_attributes: [ :bodytext ])
    end
    end