2012-10-26 711 views
1

我真的是Ruby on Rails中的新手... 我试图在我的项目中创建另一个页面的链接,其中列出了属于escuela的帖子。在Ruby on Rails中创建链接

这是我做过什么:

在posts_controller.rb我写道:

def postesc 
    @posts = Post.where(:escuela_id => params[:id]) 
    respond_to do |format| 
     format.html # postesc.html.erb 
     format.json { render json: @posts } 
    end 
    end 

在配置/ routes.rb中我写道:

match 'postesc' => 'posts#postesc' 

鉴于/ escuelas/listaesc .html.erb我写了链接:

<%= link_to "Escuelas", :controller => "posts", :action => "postesc" %> 

并在视图/ escuelas/postesc.html.erb我想列出匹配的帖子。 但是这个页面只显示空白,只有布局。

请帮忙吗?

回答

0

使routes.rb中的变化进行

得到 'postesc'=> '的帖子#postesc'

尝试... <%=的link_to “Escuelas”,postesc_path%>

OR

<%= link_to "Escuelas", { :controller => "posts", :action => "postesc" } %> 
0

你错过添加一个ID为埃斯库埃拉被选择 - 因为你在你的控制器#postesc行动做(如文字:其中:escuela_id => PARAMS [:编号]) 。

<%= link_to "Escuela", :controller => "posts", :action => "postesc", :id => 1 %> 

,但你可以使用下面的语法(通过改变你的路由豆蔻)使用对象的链接方法:

# in routes.rb 
match 'postesc' => 'posts#postesc', on: :collection, as: 'esc_index' 

# in your view 
<%- for escuela in @escuelas do %> 
    <%= link_to "Escuela", esc_index(escueal) %> 
<% end %> 
2

首先要交和ESCUELA之间的关联,那么你就可以找到它只是

Escuela.find(params[:id]).posts 

你的路线更改为 -

resources :posts do 
    get 'postesc', :on => :collection 
end 

查看:

<%= link_to "List posts", postesc_posts_path %>