Skip to content

Instantly share code, notes, and snippets.

@drager
Last active August 29, 2015 14:00
Show Gist options
  • Save drager/11259637 to your computer and use it in GitHub Desktop.
Save drager/11259637 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 do |t|
= t.label :bodytext
%div{class: 'field'}
= t.text_area :bodytext
%div{class: 'actions'}
= f.submit
<h1>
Edit topic
123123111555
</h1>
<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> <label for="topic_name">Name</label>
<div class='field'>
<input id="topic_name" name="topic[name]" type="text" value="123123111555" />
</div>
<label for="topic_posts_attributes_0_bodytext">Bodytext</label>
<div class='field'>
<textarea id="topic_posts_attributes_0_bodytext" name="topic[posts_attributes][0][bodytext]">
mmmmmmmmmmmmmmmaaaaaaaaaaaa</textarea>
</div>
<input id="topic_posts_attributes_0_id" name="topic[posts_attributes][0][id]" type="hidden" value="58" /><label for="topic_posts_attributes_1_bodytext">Bodytext</label>
<div class='field'>
<textarea id="topic_posts_attributes_1_bodytext" name="topic[posts_attributes][1][bodytext]">
mmmmmmmmmmmmmmmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</textarea>
</div>
<input id="topic_posts_attributes_1_id" name="topic[posts_attributes][1][id]" type="hidden" value="59" /><label for="topic_posts_attributes_2_bodytext">Bodytext</label>
<div class='field'>
<textarea id="topic_posts_attributes_2_bodytext" name="topic[posts_attributes][2][bodytext]">
mmmmmmmmmmmmmmmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa12312123123</textarea>
</div>
<input id="topic_posts_attributes_2_id" name="topic[posts_attributes][2][id]" type="hidden" value="60" /><label for="topic_posts_attributes_3_bodytext">Bodytext</label>
<div class='field'>
<textarea id="topic_posts_attributes_3_bodytext" name="topic[posts_attributes][3][bodytext]">
mmmmmmmmmmmmmmmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</textarea>
</div>
<input id="topic_posts_attributes_3_id" name="topic[posts_attributes][3][id]" type="hidden" value="61" />
<div class='actions'>
<input name="commit" type="submit" value="Update Topic" />
</div>
</form>
Started PATCH "/forums/ruby/topics/123123111" for 127.0.0.1 at 2014-04-25 10:19:50 +0200
Processing by TopicsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"UQL4ExZ0s+J/pZvwhXpJGK6ZxcCVuTVDsfbY+OCEkrY=", "topic"=>{"name"=>"123123111555", "posts_attributes"=>{"0"=>{"bodytext"=>"mmmmmmmmmmmmmmmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa12312123123", "id"=>"58"}, "1"=>{"bodytext"=>"mmmmmmmmmmmmmmmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "id"=>"59"}}}, "commit"=>"Update Topic", "forum_id"=>"ruby", "id"=>"123123111"}
Rails.application.routes.draw do
root to: 'categories#index'
#get 'forum/:id', to: 'forums#show', as: 'forums'
#get 'forum/:forum_id/:id', to: 'topics#show', as: 'topics'
resources :forums, only: [:index, :show] do
resources :topics do
resources :posts, only: [:index]
end
end
end
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
@topic = set_topic
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