Skip to content

Instantly share code, notes, and snippets.

@randalmaile
Last active December 31, 2015 21:49
Show Gist options
  • Save randalmaile/8049312 to your computer and use it in GitHub Desktop.
Save randalmaile/8049312 to your computer and use it in GitHub Desktop.
Bloccit - Topics and Posts
1. After ammending the seed.rb file, why don't we run db:seed? Don't understand.
2. When we are adjusting the topics show view, describe for me what passing @topic does - I didn't find anything in [ActionView::Helpers::UrlHelper](http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to) that I could decipher:
- link_to "New Post", new_topic_path(@topic), class: 'btn btn btn-large btn-block' - this was before we refactored.... I guess, that by passing the instance variable, the helper method automatically figures out the route, no?
3. Could you give me a full explanation of the posts_controller as it stands after this checkpoint. Still confused about the methods called on the attributes....
The whole point of building this piece of the app is to organize the posts - into **topics**
1. First off is to create the **model** with attributes that make sense - :name, :description, and :public
$ **rails g Topic name:string description:string public:boolean** (don't forget to associate the attributes w/ column types)
2. One of the requirements is to have the public attribute set to true automatically in the db, so change the migration file:
add **public: default:** true to t.boolean :public
3. $ **rails db:migrate**
4. Now you have to add a foreign key to your posts table and index the posts table as well:
a. $ **rails g migration AddTopicToPosts topic_id:integer:index**
3. Now you need to think about associations and tweek your models: TOPICS have 1-many relationship w/ POSTS, so:
a. Topic.rb - add: **has_many: posts**
b. Post.rb - add: **belongs:to**
c. add **:topic** to the attr_accessible method in Post.rb
4. You've created/updated your table architecture, and you've established your relationship in the model, but there's no topics and no associations, so you need to recreate them - as well as establish a new set of users with all possible permissions.
a. **Update the seed method**:
1. create and empty topics array
2. populate it with 15 topics using Faker
3. Adjust the previous ranges/loops with the Posts/comments: by setting a local variable to the topic array element and add the hash element into the u.posts.create(hash) method, where topic: topic.
5. $ **db:reset**
6. Now create the new topics controller: $ **rails g controller topics index new show edit**
7. Adjust the routes file: take out the get methods and replace w/ the **resources :topics call**
a. Run $ **rake routes** to confirm the proper set of routes/actions
8. Now you're going to have to configure the topics controller - don't forget to add the *create and update methods*:
a. Structure:
1. Index:
@topics = Topic.all (instance var set to a collection of all objects (pulled from the DB))
2. Show:
@topic = Topic.find(params[:id]) (instance var set to a single object defined by the id value in the params hash)
3. New:
@topic = Topic.new (create a empty new instance of a topic)
4. Create:
@topic = Topic.new(params[:topic]) (create a new instance populated with the submitted values from the :topic hash in the params hash)
if @topic.save
flash[:notice] = happy message
redirect_to @topic
else
flash[:error] = craft a helpful error message
render :new
5. Edit:
@topic = Topic.find(params[:id]) (instance var set to a single object defined by the id value in the params hash)
6. Update:
@topic = Topic.find(params[:id]) ((instance var set to a single object defined by the id value in the params hash)
if @topic.update_attributed(params[:topic])
flash[:notice] = happy mssg
redirect_to @topic
else
flash[:error] = helpful error
render :new
9. Update the views with the appropriate topics/posts/buttons/etc.
1. One little extra: when you show topics - you also want to show associated posts, so first:
a. topics_controller.rb: Show method: create an instance of the associated posts: @posts = @topic.posts
10. Nest the posts under topics in the routes.rb file - this will establish the url: topics/posts
```
resources :topics do
resources :posts, except: [:index]
end
```
11. Refactor and take into account that all routes from topics to posts, need a @topic parameter and all posts to topics need an @topic parameter in the link_to helper method, and in turn, the controllers need to pass the views a @topic instance variable.
@mkwiatkowski
Copy link

  1. You have to run rake db:reset at some point, which also loads the seed file.
  2. The link_to method doesn't care how the URL is generated, so that's not the right documentation page to look at here. Your questions has more to do with the routing, so rather take a look at sections of the Rails Guide on Routing: 2.3 Path and URL Helpers and 2.7 Nested Resources. The short story is that when using any of the _path helpers you need to pass an object for each parameter in the route. Here, the route is /topics/:topic_id/posts/new so you need to pass a topic object to match the :topic_id param. In case of /topics/:topic_id/posts/:id route you need to pass a topic object for :topic_id and a post object for :id. That's why you would have topic_post_path(@topic, @post) that case.
  3. I believe we did that during our last meeting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment