This is a fairly direct use of Ruby on Rails. I recommend reading Active Record Basics to speed up.
First, you should have a belongs_to relationship between messages and users, which looks like this:
class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end
This adds the .posts method to the User instance instance and .user to the Post instance.
Then you need to decide how you want the URL structure of your application to work. Here are a few options from the head:
- / user posts =: user_id
- / posts / by /: user_id
- / users /: / message ID
Given the relationship between the user and their posts, my recommendation (and I believe the overall "Rails Way") will be # 3. So add the routes to config/routes.rb :
A short way to create that route's JUST:
get 'users/:id/posts' => 'users#posts', :as => :user_posts
A long way to create a resource- based route:
resources :users do member do get :posts end end
Both approaches will provide a helper method called user_posts_path and one called user_posts_url , which you can use in your view to link to a list of user messages using the helper method link_to :
<%= link_to post.user.name, user_posts_path(post.user) %>
Now you need to add the controller action to app/controllers/users_controller.rb :
class UsersController < ActionController::Base def posts @user = User.find(params[:id]) @posts = @user.posts end end
and then add the HTML / ERB code in app/views/users/posts.html.erb
<% @posts.each do |post| %> <%= post.inspect %> <% end %>
This should give you the basic ability to display user messages. You can improve it by reusing partial parts or some other shortcuts, but I will leave this as an exercise for you to understand.