How can I get all messages from a specific user

I create my own blog in Rails with posts and users. I need to show all posts from a specific author when I click on it (here is the concept: link ). What should I do for this? Tell me what additional information or code should be added.

users_controller:

class UsersController < ApplicationController def show @user = User.find(params[:id]) @posts = @user.posts end end 

posts_controller:

 class PostsController < ApplicationController before_filter :authenticate_user!, :except => [:show, :index] # GET /posts # GET /posts.json def index @posts = Post.all respond_to do |format| format.html # index.html.erb format.json { render json: @posts } end end # GET /posts/1 # GET /posts/1.json def show @post = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render json: @post } end end # GET /posts/new # GET /posts/new.json def new @post = Post.new respond_to do |format| format.html # new.html.erb format.json { render json: @post } end end # GET /posts/1/edit def edit @post = Post.find(params[:id]) end # POST /posts # POST /posts.json def create #@post = Post.new(params[:post]) @post = current_user.posts.build(params[:post]) respond_to do |format| if @post.save format.html { redirect_to @post, notice: 'Post was successfully created.' } format.json { render json: @post, status: :created, location: @post } else format.html { render action: "new" } format.json { render json: @post.errors, status: :unprocessable_entity } end end end # PUT /posts/1 # PUT /posts/1.json def update @post = Post.find(params[:id]) respond_to do |format| if @post.update_attributes(params[:post]) format.html { redirect_to @post, notice: 'Post was successfully updated.' } format.json { head :no_content } else format.html { render action: "edit" } format.json { render json: @post.errors, status: :unprocessable_entity } end end end # DELETE /posts/1 # DELETE /posts/1.json def destroy @post = Post.find(params[:id]) @post.destroy respond_to do |format| format.html { redirect_to posts_url } format.json { head :no_content } end end end 

user model:

 class User < ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable has_many :posts, :dependent => :destroy validates :fullname, :presence => true, :uniqueness => true validates :password, :presence => true validates :email, :presence => true, :uniqueness => true devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :fullname end 

post model:

 class Post < ActiveRecord::Base attr_accessible :text, :title validates :user_id, :presence => true validates :title, :presence => true validates :text, :presence => true belongs_to :user has_many :comments end 
+6
source share
2 answers

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.

+13
source

You need 2 models: User and Mail. There is a connection between them: the HAS MANY user publishes messages, sends BELONGS TO to the user. To create this relationship in the database, you must add the user_id table to the posts table. To do this, simply run the following command:

 rails generate migration AddUserIdToPosts user_id: integer 

Remember to run rake db: after that, migrate

To create a relationship between the models, add the User model:

 has_many :posts, dependent: :destroy 

And to publish the model:

 belongs_to :user 

Now you can use the "custom" method for messages and the "messages" for the user. For example, in the demo action of the user controller:

 @user = User.find(params[:id]) @posts = @user.posts 

These links will help you: http://guides.rubyonrails.org/association_basics.html http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

+1
source

Source: https://habr.com/ru/post/950476/


All Articles