1

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.

marios
  • 261
  • 8
  • 26

2 Answers2

1

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?

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • i did what you say and it says undefined local variable or method `admin_id' – marios Dec 11 '13 at 11:08
  • lol sorry - I meant those as guidelines -- the `admin_id` and `post_id` variables need to be from your code. I.E whatever variables in your code represent `admin_id` and `post_id` – Richard Peck Dec 11 '13 at 11:13
  • I really don’t understand what you mean by the last comment. I have posted all my code above. please can you be more specific with the admin_id and post_id? On admin_id i try to put _name_ but still didnt work. – marios Dec 11 '13 at 11:34
  • I meant that I gave you a guideline of where to put _your_ variables. Your "admin_id" variable might be `current_user.id` – Richard Peck Dec 11 '13 at 11:36
  • ohh i see. But i need link_to kosh, for simple users that they dont login. i need it static. – marios Dec 11 '13 at 11:39
  • this what you are saying on your answer is dynamic right? @Rich – marios Dec 11 '13 at 11:57
  • It's only dynamic in the sense that you've got to provide the variables to make it happen. If you wanted it to be static, there's nothing stopping you putting the variables in as strings, like: `link_to users_posts_path("13", "12")` – Richard Peck Dec 11 '13 at 11:59
  • look my edit on my question and please let me know – marios Dec 18 '13 at 14:14
1

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
nickcen
  • 1,682
  • 10
  • 10
  • can you explane your answer? where i put this and do i have to make resources :users do resources :posts end ?that is _blank ? – marios Dec 11 '13 at 19:47
  • @marios 1. place this script in the html.erb file where you wanna to output links. 2. yes , you have to defined the resources. 3. _blank means when user click on the link, it will show the destination page in a new browser window instead of replace the current one. – nickcen Dec 12 '13 at 00:21
  • I did what you say and is not showing me anything. I put the script in layout application. And yes i have admin with ID 1 – marios Dec 12 '13 at 09:54
  • @marios try this <%= ink_to user.name, user_posts_path(user), :target => '_blank' %>. remove the space between <% and =, and change the path to user_posts_path. – nickcen Dec 12 '13 at 10:40
  • i did it, and nothing changed.Something is wrong with the User.where("admin=1").each – marios Dec 12 '13 at 11:54
  • input the command 'rails c' in your console, type the statement 'User.where("admin=1")' and see whether it return your admin user lists. – nickcen Dec 13 '13 at 01:51
  • if i put User.where("admin=1") it says => # . But if i put User.where("") it says => #, – marios Dec 16 '13 at 09:39
  • i put in the layout the User.where("") and it worked but now i cant see any posts. it take me on /users/1/posts and doent show anything. – marios Dec 16 '13 at 10:00
  • which db are you using, can you show me the record inside db. It looks like your db didn't store 1 for the true value. – nickcen Dec 16 '13 at 10:01
  • it stores it because if you see the comment above last line it says admin: true. The db is sqlite3. But anw now i have make it and shows me the admin names, But when i press it doesnt show me any posts from this user and i am sure that i have posts – marios Dec 16 '13 at 10:05
  • i know that i have to make some changes in view>posts> but i dont now what also when takes me to user/1/posts i know is missing one parameter (the id of the post) but i want all the posts from this user so how i can do this. please let me know if you know so i can accept your answer – marios Dec 16 '13 at 10:23
  • @marios i have update my code to get back admin user list and save post, take a look. And show me your db records of db table posts. – nickcen Dec 16 '13 at 13:08
  • SELECT "posts".* FROM "posts" => #, #]> – marios Dec 16 '13 at 16:13
  • now when i press the link "kosh" it takes me on views/posts/index.html.erb with the error that i have edit the question above – marios Dec 16 '13 at 16:48
  • 1
    1. From you db record, it seems you store the username instead of user_id in the posts table. That is because you assign the session[:user_name] to the new post user_id field. Which i think this is not right. 2. Inside the posts_controller's index method, instead of getting back all the posts, i think you should get back the user first, then get posts of the specified user. 3. According to your routes definition, the post's path should be user_post_path(user, post) – nickcen Dec 16 '13 at 23:40
  • so what i have to put in postcontroller index to get the post only for that user? – marios Dec 17 '13 at 10:03
  • i have make this way user=User.find_by_name(:user_id) @posts=user. **posts** , and i am getting the correct id but says error on undefined method `posts' – marios Dec 17 '13 at 17:48
  • i have update the code in question to be more clear so if you want have a look – marios Dec 18 '13 at 11:51