从另一个控制器渲染显示视图
Render show view from another controller
我已经预览了所有具有相似主题的问题,并且 none 这些解决方案对我有帮助。我正在尝试创建一个类似推特的提要,它将在 rails 中显示某个类别的 post。
这是我的行业主管:
class IndustriesController < ApplicationController
def index
@wads = Wad.order('created_at DESC')
end
def music
@music_wads = Wad.where(category: "Music").paginate(page: params[:page], per_page: 20)
@wad = @music_wads.pluck(:id)
end
end
这是我的 post 控制器的一部分:
class WadsController < ApplicationController
before_action :find_wad, only: [:show, :edit, :update, :destroy]
def index
@wads = Wad.all
end
def show
@wad = Wad.find(params[:id])
end
def new
@wad = Wad.new
end
def create
@wad = current_user.wads.build(wad_params)
if @wad.save
redirect_to @wad
else
flash[:error] = 'Error try again'
render 'new'
end
end
end
这是我的行业控制器的显示视图:
<h1>Music Wads</h1>
<%= will_paginate @music_wads %>
<% @music_wads.each do |music_wad| %>
<%= link_to 'wads/:id' do %>
<div class="flex-rectangle">
<%= music_wad.user.name %>
<%= music_wad.short_form %>
<% end %>
</div>
<%= will_paginate @music_wads %>
<% end %>
这是我的路线文件:
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/industries', to: 'industries#index'
get '/music', to: 'industries#music'
get '/tech', to: 'industries#tech'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :wads
end
我正试图做到这一点,以便在 post 列表中单击 post 会将您带到该 post (/wads/id) 的页面。我一整天都感到困惑,现在束手无策。我知道我是一个菜鸟,这是一个菜鸟问题,但我们很乐意提供任何帮助。
谢谢
resources :wads
将创建一些您可以使用的路线。
控制台中的 运行 rails routes
(在 rails 5 上)或 rake routes
(在 rails 4 及更低版本上)将为您提供列表你的路线。
在 prefix
栏下,您可以找到您应该使用的正确路线名称,在 URI Pattern
栏下,您可以看到它 link 到达的实际地址。
你要求 wads/:id
所以 link 应该是 <%= link_to wad_path(wad_music) do %>
(前缀是 wad_path
你需要给它包含 [=18 的对象=] - 或者一个 id...)
因为你想 link 一个单一的动作 - 这意味着该动作将获取一个对象的 ID - 并获取它(获取单个对象!) link 前缀将是单数形式:wad_path
而不是 wads_path
(wads_path
将 link 到控制器中的 index
操作,不需要获取任何对象或 ID)
我已经预览了所有具有相似主题的问题,并且 none 这些解决方案对我有帮助。我正在尝试创建一个类似推特的提要,它将在 rails 中显示某个类别的 post。 这是我的行业主管:
class IndustriesController < ApplicationController
def index
@wads = Wad.order('created_at DESC')
end
def music
@music_wads = Wad.where(category: "Music").paginate(page: params[:page], per_page: 20)
@wad = @music_wads.pluck(:id)
end
end
这是我的 post 控制器的一部分:
class WadsController < ApplicationController
before_action :find_wad, only: [:show, :edit, :update, :destroy]
def index
@wads = Wad.all
end
def show
@wad = Wad.find(params[:id])
end
def new
@wad = Wad.new
end
def create
@wad = current_user.wads.build(wad_params)
if @wad.save
redirect_to @wad
else
flash[:error] = 'Error try again'
render 'new'
end
end
end
这是我的行业控制器的显示视图:
<h1>Music Wads</h1>
<%= will_paginate @music_wads %>
<% @music_wads.each do |music_wad| %>
<%= link_to 'wads/:id' do %>
<div class="flex-rectangle">
<%= music_wad.user.name %>
<%= music_wad.short_form %>
<% end %>
</div>
<%= will_paginate @music_wads %>
<% end %>
这是我的路线文件:
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/industries', to: 'industries#index'
get '/music', to: 'industries#music'
get '/tech', to: 'industries#tech'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :wads
end
我正试图做到这一点,以便在 post 列表中单击 post 会将您带到该 post (/wads/id) 的页面。我一整天都感到困惑,现在束手无策。我知道我是一个菜鸟,这是一个菜鸟问题,但我们很乐意提供任何帮助。 谢谢
resources :wads
将创建一些您可以使用的路线。
运行 rails routes
(在 rails 5 上)或 rake routes
(在 rails 4 及更低版本上)将为您提供列表你的路线。
在 prefix
栏下,您可以找到您应该使用的正确路线名称,在 URI Pattern
栏下,您可以看到它 link 到达的实际地址。
你要求 wads/:id
所以 link 应该是 <%= link_to wad_path(wad_music) do %>
(前缀是 wad_path
你需要给它包含 [=18 的对象=] - 或者一个 id...)
因为你想 link 一个单一的动作 - 这意味着该动作将获取一个对象的 ID - 并获取它(获取单个对象!) link 前缀将是单数形式:wad_path
而不是 wads_path
(wads_path
将 link 到控制器中的 index
操作,不需要获取任何对象或 ID)