1

I'm really really newbie in Ruby on Rails... I'm trying to make a link to another page in my project, where it's listed the posts that belong to an escuela.

This is what I did:

In posts_controller.rb I wrote:

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

In config/routes.rb I wrote:

match 'postesc' => 'posts#postesc'

In view/escuelas/listaesc.html.erb I wrote the link:

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

And in view/escuelas/postesc.html.erb I want to make a list of the matching posts. But this page appears just blank, with only the layout.

Please, some help?

3 Answers 3

2

First make the association between post and escuela, then you can find it just by

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

Change your routes to -

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

View :

<%= link_to "List posts", postesc_posts_path %>
Sign up to request clarification or add additional context in comments.

Comments

0

make a change in routes.rb as

get 'postesc' => 'posts#postesc'

try...<%= link_to "Escuelas", postesc_path %>

OR

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

Comments

0

you're missing to add an ID for the Escuela to be selected - as you're doing in your Controller#postesc Action (as in words: where: escuela_id => params[:id]).

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

but you could use the object-link method using the following syntax (by changing your routes a litte):

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.