0

I have nested resources like that :

resources :cards do
    resources :teammates
    resources :formata, only: [:index, :show]
end
  • I use formata#show to render an alternative card#show
  • Cards has_many teammates

QUESTION : I would like to display all the infos available inside Cards table (city, place, ...), and the infos of the Teammates belonging to the card (firstname, lastname) => in the formata#show view.

I do not know how to set the strong parameters to work well.

For the moment I have in formata controller :

class FormataController < ApplicationController
 before_filter :authenticate_crafter!
 before_action :set_card, only: [:show]

 def index
  render ("formata/show")
 end

 def show
  @card = Card.find(params[:card_id])
  @teammates = Teammates.where(card_id: @card).take!
 end

 private
  def set_card
   @card = Card.find(params[:card_id])
  end

 def formata_params
  params.require(:card).permit(:content, :city, :place)
 end
end

Triggered with url like this :

http://www.appname.com/cards/:id_card/formata

And my view formata/show.html.erb contains:

City <%= @Card.city %>
Place <%= @Card.place %>

Rendering view error:

undefined method `city' for nil:NilClass

None of my 2 variables @card & @teammates work in formata#show. And work well in #teammates (same level of nest).

Any help would be much appreciated. Thank you

nhahtdh
  • 55,989
  • 15
  • 126
  • 162

1 Answers1

1

it is case sensitive. card should be in small case as it is coming from controler

<%= @Card.city %> 

change it to

<%= @card.city %>
Nitin Jain
  • 3,053
  • 2
  • 24
  • 36
  • Unfortunately it was a typo on stackoverflow, It is already @card inside view :/ My bad – user3113259 Dec 24 '13 at 16:25
  • one more thing why are you doing this twice @card = Card.find(params[:card_id]) one in show and second through set_card – Nitin Jain Dec 24 '13 at 16:28
  • No reason about redundacy of card = Card.find(params[:card_id]), just have the habit to precise it in each action, and delete it at the end. On interesting thing is that without "render" in index action, and copy-past of my formata#show view inside my formata#index view, card is loaded proprely and displays info. Teammates not. So I guess there is a problem with my teammates request in controller and a problem in passign parameters card and teammates with the render. – user3113259 Dec 24 '13 at 16:50
  • Here is my formata#show view as requested: Test show view <%= @card.place %> – user3113259 Dec 24 '13 at 16:52