Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get all posts from a specific user
    primarykey
    data
    text
    <p>I'm creating my own blog on Rails with posts and users. I need to show all posts from specific author when I click on him (here the concept:<a href="http://i.imgur.com/RqUDwZj.png">link</a>). What should I do for this? Please say what extra information or code should I add</p> <p>users_controller:</p> <pre><code>class UsersController &lt; ApplicationController def show @user = User.find(params[:id]) @posts = @user.posts end end </code></pre> <p>posts_controller:</p> <pre><code>class PostsController &lt; ApplicationController before_filter :authenticate_user!, :except =&gt; [: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 </code></pre> <p>user model:</p> <pre><code>class User &lt; ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable has_many :posts, :dependent =&gt; :destroy validates :fullname, :presence =&gt; true, :uniqueness =&gt; true validates :password, :presence =&gt; true validates :email, :presence =&gt; true, :uniqueness =&gt; true devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :fullname end </code></pre> <p>post model:</p> <pre><code>class Post &lt; ActiveRecord::Base attr_accessible :text, :title validates :user_id, :presence =&gt; true validates :title, :presence =&gt; true validates :text, :presence =&gt; true belongs_to :user has_many :comments end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload