Different view pages

138 views Asked by At

I want in application>layout to have something like " link_to kosh,.... " so a simple user can view all the posts that only kosh has posted. What changes i have to make,is that possible ??

senario: i am a simple user and when i go to the web i see a link_to (button) kosh (admin name) and then i press it i can see all the posts that kosh has make.

p.s: kosh is one of the admins, i will have 2-3 admins.Is ROR 4

post>controller

class PostsController < ApplicationController

before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :authorize_admin!, except: [:index, :show]

        def index
                @posts=Post.all
        end
        def new
                @post = Post.new
                  @post.user_id = session[:user_name]
        end

        def create
        @post = Post.new(post_params)

            if @post.save
        flash[:notice] = "Post has been created."
        redirect_to @post 
        else
        flash[:alert] = "Post has not been created."
        render 'new'
        end
        end

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

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

        def update
                @post = Post.find(params[:id])
                if @post.update(post_params)
                flash[:notice] = "Post has been updated."
                redirect_to @post
                else
                flash[:alert] = "Post has not been updated."
                render "edit"
                end
        end
        def destroy
                @post = Post.find(params[:id])
                @post.destroy
                flash[:notice] = "Post has been destroyed."
                redirect_to posts_path
        end


private
        def post_params
                params.require(:post).permit(:title, :description,:prediction,:user_id)
        end

        def set_post
                @post = Post.find(params[:id])
                rescue ActiveRecord::RecordNotFound
                flash[:alert] = "The post you were looking" +
                " for could not be found."
                redirect_to posts_path
        end

end

post>model

class Post < ActiveRecord::Base
belongs_to :user
validates :title, presence: true

end

user>model

class User < ActiveRecord::Base
        has_secure_password
        has_many :posts

end

post>db

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :title
      t.string :description
      t.string :user_id
      t.timestamps
    end
  end
end

user>db

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email
      t.string :password_digest
      t.boolean :admin

      t.timestamps
    end
  end
end

routes

  resources :posts
  resources :users

EDIT the code

I have make some changes but still not working. I was able to show the link for each admin but not able to get the posts from that specific admin only

in routes

resources :users do
    resources :posts
end

in postscontroller

 def index
             @user = User.find(params[:user_id])
            @posts = Post.all
        end

    def create
         @user = User.find_by_name(session[:user_name])
        @post = Post.new(post_params)

        if @post.save
        flash[:notice] = "Post has been created."
        redirect_to user_post_path(@user,@post)
        else
        flash[:alert] = "Post has not been created."
        render 'new'
        end
    end

in application>layout (this is how i get the links)

<% User.where(:admin=>true).each do |user| %>
 <li> <%= link_to user.name, user_posts_path(user) %> </li>
<% end %>

view>posts>index

<h2>Posts</h2>
<ul>
<% if @posts.present? %>
<% @posts.each do |post| %>
<li><%= link_to post.title %></li>
By: <%= post.user_id%>
<% end %>
</ul>
<%else%>
You don't have any products yet.
<%end%>


<% admins_only do %>
<%= link_to "New Post", new_user_post_path %>
<%end%>

in the controller index i have try to put

@user = User.find(:user_id)
@posts = @user.posts

BUT says undefined posts.

2

There are 2 answers

18
nickcen On BEST ANSWER

for sqlite use User.where(:admin => true) to get back all the admin users, and generate a link for each of them

<% User.where(:admin => true).each do |user| %>
  <%= link_to user.name, user_posts_path(user), :target => '_blank' %>
<% end %>

The code create posts belongs to a specified user, i think the code should looks something like this

class PostController
  def index
    user = User.find_by_name(session[:user_name])
    @posts = user.posts
  end
  def create
    user = User.find_by_name(session[:user_name])
    user.posts.create(params)
  end
end
8
Richard Peck On

Yes it's possible

The way you'd do this is to use nested resources, which will look like this:

#config/routes.rb
resources :users do
    resources :posts
end

This will create routes which look like this:

/app/users/:user_id/posts
/app/users/:user_id/posts/:post_id

This will then allow you to link to these routes like this:

<%= link_to users_posts_path(admin_id, post_id) %>

This will load up the posts belonging to that user

Hope this helps?