Note that there are some explanatory texts on larger screens.

plurals
  1. PODifferent view pages
    primarykey
    data
    text
    <p>I want in <strong>application>layout</strong> 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 ??</p> <p>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> <p><strong>p.s: kosh is one of the admins, i will have 2-3 admins.Is ROR 4</strong></p> <p>post>controller</p> <pre><code>class PostsController &lt; 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 </code></pre> <p>post>model</p> <pre><code>class Post &lt; ActiveRecord::Base belongs_to :user validates :title, presence: true end </code></pre> <p>user>model</p> <pre><code>class User &lt; ActiveRecord::Base has_secure_password has_many :posts end </code></pre> <p>post>db</p> <pre><code>class CreatePosts &lt; ActiveRecord::Migration def change create_table :posts do |t| t.string :title t.string :description t.string :user_id t.timestamps end end end </code></pre> <p>user>db</p> <pre><code>class CreateUsers &lt; 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 </code></pre> <p>routes </p> <pre><code> resources :posts resources :users </code></pre> <p><strong>EDIT the code</strong></p> <p>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 </p> <p>in routes</p> <pre><code>resources :users do resources :posts end </code></pre> <p>in postscontroller</p> <pre><code> 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 </code></pre> <p>in application>layout (this is how i get the links)</p> <pre><code>&lt;% User.where(:admin=&gt;true).each do |user| %&gt; &lt;li&gt; &lt;%= link_to user.name, user_posts_path(user) %&gt; &lt;/li&gt; &lt;% end %&gt; </code></pre> <p>view>posts>index</p> <pre><code>&lt;h2&gt;Posts&lt;/h2&gt; &lt;ul&gt; &lt;% if @posts.present? %&gt; &lt;% @posts.each do |post| %&gt; &lt;li&gt;&lt;%= link_to post.title %&gt;&lt;/li&gt; By: &lt;%= post.user_id%&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;%else%&gt; You don't have any products yet. &lt;%end%&gt; &lt;% admins_only do %&gt; &lt;%= link_to "New Post", new_user_post_path %&gt; &lt;%end%&gt; </code></pre> <p>in the controller index i have try to put </p> <pre><code>@user = User.find(:user_id) @posts = @user.posts </code></pre> <p>BUT says undefined posts.</p>
    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.
    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