Skip to content

Instantly share code, notes, and snippets.

@workmad3
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save workmad3/11283154 to your computer and use it in GitHub Desktop.
Save workmad3/11283154 to your computer and use it in GitHub Desktop.
= 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 :posts, post do |p|
= render :partial => "posts/form", :locals => { :f => p }
%div{class: 'actions'}
= f.submit
- if @topic.new_record?
asd
%h1
Edit topic
= @topic.name
= render 'form', post: @post
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"}
%h1 New topic
= render 'form', post: @topic.posts.first
= f.label :bodytext
%div{class: 'field'}
= f.text_area :bodytext
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
@post = @topic.posts.first
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment