Daniel Wagner
Daniel Wagner

Reputation: 13

rails newbie: how to get params from view to controller

i've been thinking about this way to long. I'm pretty sure that you will find my mistake super fast. As i've read at least ten similar threads/questions with nearly the same "problem" I think I figured what "the problem" might be, but don't know how to solve it.

my question after thinking like three hours about it: why doesn't my editview recieve the params of <%= link_to 'bearbeiten', edit_tour_tn_path(@tour, t) % ?

in detail:

I'am working with nested resources (as described in the getting started guide): one tour has_many tns, one tn belongs_to a tour.

routes.rb

resources :tours do 
  resources :tns  
end

tns_controller.rb

class TnsController < ApplicationController

  def create
   @tour = Tour.find(params[:tour_id])
   @tn = @tour.tns.create(tn_params)
   redirect_to tour_path(@tour)
  end

 def new
  @tour = Tour.find(params[:tour_id])
  @tn = @tour.tns.create(tn_params)
 end

 def index
  @tour = Tour.find(params[:tour_id])
  @tns = @tour.tns.all
 end

def edit
  @tour = Tour.find(params[:tour_id])
  @tn = @tour.tns.find(params[:id])

 if @tn.update(tn_params)
   redirect_to tour_tns_path
 else
   render 'edit'
 end
end

def show
  @tn = Tn.find(params[:id])
end

private
 def tn_params
  params.require(:tn).permit(:vorname, :nachname, :gender, :bdate, :email, :telefon, :schwimmen, :besonderheit, :eek, :fotog, :notfallk, :kkh)
end

end

view: form_for snippet:

  <%= form_for([@tour, @tour.tns.build]) do |f| %>
   #with many more fields, but i think they aren't relevant for my problem

view: show tour and its tns (like 'show blogpost and comments' in the tutorial)

<p> TeilnehmerInnen:
    <table>
    <th>Vorname</th>
    <th>Nachname</th>
    <th>Alter</th>
    <th>Bearbeiten</th>
    <th>Löschen</th>

<tr><% @tour.tns.each do |t| %>
<td><%= t.vorname %> </td>
<td><%= t.nachname %></td>
<td><%= ((DateTime.now - t.bdate)/ 365.25).to_i %> Jahre</td>
<td><%= link_to 'edit', edit_tour_tn_path(tour_id: t) %></td>
<td>#mülleimer verlinken</td>
</tr>   
    <% end %>
 </table>
</p>

if i click the 'bearbeiten' link to edit a tour_tn, i get the popular

"param is missing or the value is empty: tn"

referring to the private def tn_params thing:

app/controllers/tns_controller.rb:35:in tn_params' app/controllers/tns_controller.rb:22:inedit'

but as the request goes along with the parameters

Parameters:

{"tour_id"=>"3", "id"=>"5"}

i don't understand how my edit form (which is rendering the above mentioned form_for) doesn't get the param.

Could you be so kind and show me the tree in front of my nose? :>

kind regards,

Daniel

Upvotes: 1

Views: 2274

Answers (1)

vipin
vipin

Reputation: 2510

try this :

this will pass id into your edit action

<td><%= link_to 'edit', edit_tour_tn_path(t) %></td>

if you want to sent some extra parameter using

<td><%= link_to 'edit', edit_tour_tn_path(id: t) %></td>

edit action

def edit
  @tour = Tour.find(params[:tn][:tour_id])
  @tn = @tour.tns.find(params[:id])

 if @tn.update(tn_params)
   redirect_to tour_tns_path
 else
   render 'edit'
 end
end

Upvotes: 1

Related Questions