2013-12-11 20 views
1

我想在應用>佈局有類似「的link_to信貸基金,......」這樣一個簡單的用戶可以查看只信貸基金已發佈的所有帖子。我必須做出什麼改變,這是可能的?我是一個簡單的用戶,當我去網絡上時,我看到一個link_to(按鈕)kosh(管理員名稱),然後我按下它,我可以看到kosh所做的所有帖子。不同的視圖頁

PS:信貸基金是管理員之一,我將有2-3 admins.Is 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 

交>模型

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

end 

用戶>型號

class User < ActiveRecord::Base 
     has_secure_password 
     has_many :posts 

end 

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

用戶> 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 

路線

resources :posts 
    resources :users 

編輯代碼

我必須做出一些改變,但仍然沒有工作。我能夠顯示的鏈接,每個管理員,但沒能獲得從具體管理職位只在路線

resources :users do 
    resources :posts 
end 

在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 
應用

>佈局 (這是我如何得到的鏈接)

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

,但表示不確定的職位。

回答

1

SQLite的使用User.where(:管理=>真)找回所有的管理員用戶,並生成一個鏈接,他們每個人的

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

你能解釋你的答案?我把這個和我必須做的資源:用戶做資源:職位結束?這是_blank? – marios

+0

@marios 1.將此腳本放置在您想要輸出鏈接的html.erb文件中。是的,你必須定義資源。 3. _blank表示當用戶點擊鏈接時,它會在新的瀏覽器窗口中顯示目標頁面,而不是替換當前頁面。 – nickcen

+0

我做了你說的話,並沒有向我展示任何東西。我把腳本放在佈局應用程序中。是的,我有管理ID 1 – marios

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

這將加載屬於該用戶的帖子

希望這會有幫助嗎?

+0

和它說未定義的局部變量或方法'admin_id」 – marios

+0

笑對不起 - 我的意思是那些爲指導 - 在'admin_id'和'post_id'變量需要來自您的代碼。 I.E代碼中的任何變量代表'admin_id'和'post_id' –

+0

我真的不明白你最後一條評論的意思。我已經發布了我上面的所有代碼。請你可以更具體的admin_id和post_id?在admin_id我試着把_name_但仍然沒有工作。 – marios