我想在應用>佈局有類似「的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
,但表示不確定的職位。
你能解釋你的答案?我把這個和我必須做的資源:用戶做資源:職位結束?這是_blank? – marios
@marios 1.將此腳本放置在您想要輸出鏈接的html.erb文件中。是的,你必須定義資源。 3. _blank表示當用戶點擊鏈接時,它會在新的瀏覽器窗口中顯示目標頁面,而不是替換當前頁面。 – nickcen
我做了你說的話,並沒有向我展示任何東西。我把腳本放在佈局應用程序中。是的,我有管理ID 1 – marios