Mike Legacy
Mike Legacy

Reputation: 1066

Relating posts to topics in ruby on rails forum application (high level overview)

Building a forum in ruby for fun and to learn the language. To start this off, i understand basic constructs, but I am very new to server-side languages and am primarily a front-end developer. I am trying to extend my skills.

I don't necessarily want you to code for me (although code examples would be appreciated), but I would like you to explain to me why my code is terrible, which I'm sure it is and tell me how to fix it. Just need some help understand how to relate two models togethers and how to set up that relation in the controllers.

Thanks!

Here are my two models:

Post model:

class Post < ActiveRecord::Base
    belongs_to :topic
end

Topic model:

class Topic < ActiveRecord::Base
    belongs_to :user
    has_many :posts
end

Now here come the controllers. These are where I am really lost. I got the topic creation working, and I tried to just copy what I did in the topic controller. I pretty much knew it wasn't going to work, but I am sorta lost. Here it is...

Topic Controller

class TopicsController < ApplicationController
  def index
    @topics = Topic.order("sticky desc")
  end

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

  def new
    @topic = Topic.new
  end

  def create
    @topic = Topic.new(topic_params)
    @topic.user = current_user
    if @topic.save
      redirect_to @topic, notice: "Created topic."
    else
      render :new
    end
  end

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

  def update
    @topic = Topic.find(params[:id])
    if @topic.update_attributes(topic_params)
      redirect_to topics_url, notice: "Updated topic."
    else
      render :edit
    end
  end

  def destroy
    @topic = Topic.find(params[:id])
    @topic.destroy
    redirect_to topics_url, notice: "Destroyed topic."
  end

private

  def topic_params
    params.require(:topic).permit(:name, :post_content, :sticky)
  end
end

Posts Controller

class PostsController < ApplicationController

  def index
    @posts = Post.order("sticky desc")
  end

  def show
    @post = Post.find(params[:id])
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)
    @post.user = current_user
    if @post.save
      redirect_to topics_url, notice: "Post created."
    else
      render :new
    end
  end

  def edit
    @post = Post.find(params[:id])
  end

  def update
    if @post = Post.find(params[:id])
      redirect_to topics_url, notice: "Updated post."
    else
      render :edit
    end
  end

  def destroy
    @post = Post.find(params[:id])
    @post.destroy
    redirect_to topics_url, notics: "Post removed."
  end

private

  def post_params
    params.require(:posts).permit(:content, :created_at, :updated_at)
  end
end

I don't believe the views are an issue, and I will post a new question if there is once I get the controller logic figured out.

Again, any help would be appreciated. Please just no comments like, "you should really start back at the beginning", or "you aren't experienced enough, learn this first", because I know I am not experienced, hence why I am here asking you all.

You can show how you would code it, or just explain the logic that needs implemented to me, either is appreciated!

Thanks a ton everyone!

EDIT

I am getting a routing error actually. So obviously the routing is wrong, wasn't sure if it had something to do with the controller code tho: Here is the specific error. (this occurs when I try to click into a topic (I can edit and destroy topics, just not click into them)

Routing Error

No route matches {:action=>"new", :controller=>"posts"}
Try running rake routes for more information on available routes.

Here is my routes files so far:

Forum::Application.routes.draw do


  get 'signup', to: 'users#new', as: 'signup'
  get 'login', to: 'sessions#new', as: 'login'
  get 'logout', to: 'sessions#destroy', as: 'logout'

  resources :sessions
  resources :topics do
    resources :posts
  end
  resources :users


  root to: 'topics#index'
end

Upvotes: 0

Views: 431

Answers (1)

chitra sureworks
chitra sureworks

Reputation: 11

Serve is a little Rack-based web server that makes it simple to serve ERB or HAML from any index. Serve is an optimal apparatus for building HTML models of Rails applications. Serve can likewise deal with SASS, Textile, and Markdown in the event that the suitable diamonds are introduced. enter image description here

Upvotes: 1

Related Questions