不同的视图页面

时间:2013-12-11 10:07:32

标签: ruby-on-rails post ruby-on-rails-4 views nested-resources

我想在应用程序>布局中使用类似“link_to kosh,....”的内容,以便简单的用户可以查看只有kosh发布的所有帖子。我必须做出哪些改变,这是可能的吗?

senario:我是一个简单的用户,当我进入网络时,我看到一个link_to(按钮)kosh(管理员名称)然后我按下它我可以看到kosh所做的所有帖子。

p.s:kosh是管理员之一,我将有2-3名管理员。是ROR 4

交&分解控制器

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

交&GT;模型

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

end

使用者&gt;模型

class User < ActiveRecord::Base
        has_secure_password
        has_many :posts

end

交&GT;分贝

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

使用者&gt;分贝

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

路线

  resources :posts
  resources :users

编辑代码

我做了一些改动,但仍然无法正常工作。我能够显示每个管理员的链接,但无法从该特定管理员那里获得帖子

在路线

resources :users do
    resources :posts
end
postcontroller中的

 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

在应用程序&gt;布局中 (这就是我获取链接的方式)

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

视图&gt;&帖GT;索引

<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%>

在控制器索引中我试图把

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

但是说未定义的帖子。

2 个答案:

答案 0 :(得分:1)

是的,可能

您执行此操作的方式是使用nested resources,如下所示:

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

这将创建如下所示的路线:

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

这将允许您链接到这些路线:

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

这将加载属于该用户的帖子

希望这有帮助吗?

答案 1 :(得分:1)

for sqlite使用User.where(:admin =&gt; true)来取回所有管理员用户,并为每个用户生成一个链接

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

代码创建帖子属于指定用户,我认为代码看起来应该是这样的

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