【问题标题】:Different view pages [closed]不同的视图页面[关闭]
【发布时间】:2013-12-11 10:07:31
【问题描述】:

我希望在 application>layout 中有类似“link_to kosh,....”的内容,这样一个简单的用户就可以查看只有 kosh 发布的所有帖子。我必须做出哪些改变,这可能吗??

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

p.s:kosh 是其中一位管理员,我将有 2-3 个管理员。是 ROR 4

post>控制器

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

发布>模特

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

end

用户>模型

class User < ActiveRecord::Base
        has_secure_password
        has_many :posts

end

发布>数据库

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

用户>数据库

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

在帖子控制器中

 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

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

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

查看>帖子>索引

<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

但是说未定义的帖子。

【问题讨论】:

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


    【解决方案1】:

    对于 sqlite 使用 User.where(:admin => 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
    

    【讨论】:

    • 你能解释一下你的答案吗?我把这个放在哪里,我是否必须制作资源:用户做资源:帖子结束?那是_blank?
    • @marios 1. 将此脚本放在要输出链接的 html.erb 文件中。 2. 是的,你必须定义资源。 3. _blank 表示当用户点击链接时,它将在新的浏览器窗口中显示目标页面,而不是替换当前页面。
    • 我照你说的做了,没有给我看任何东西。我将脚本放在布局应用程序中。是的,我有 ID 为 1 的管理员
    • @marios 试试这个 '_blank' %>。删除
    • 1.从您的数据库记录来看,您似乎将用户名而不是 user_id 存储在帖子表中。那是因为您将 session[:user_name] 分配给新的 post user_id 字段。我认为这是不对的。 2. 在posts_controller 的index 方法里面,我认为你应该先取回用户,而不是取回所有帖子,然后再获取指定用户的帖子。 3.根据你的路由定义,post的路径应该是user_post_path(user, post)
    【解决方案2】:

    是的,有可能

    您可以使用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) %>
    

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

    希望这有帮助吗?

    【讨论】:

    • 我照你说的做了,它说未定义的局部变量或方法`admin_id'
    • 大声笑对不起 - 我的意思是那些作为指导 - admin_idpost_id 变量需要来自您的代码。 IE 代码中的任何变量都代表 admin_idpost_id
    • 我真的不明白你最后的评论是什么意思。我已经在上面发布了我的所有代码。请你能更具体地使用 admin_id 和 post_id 吗?在 admin_id 上,我尝试输入 name 但仍然无效。
    • 我的意思是我给了你一个放置你的变量的指南。您的“admin_id”变量可能是 current_user.id
    • 哦,我明白了。但我需要link_to kosh,对于他们不登录的简单用户。我需要它是静态的。
    猜你喜欢
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2014-06-13
    • 2021-12-10
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多