klasarn
klasarn

Reputation: 129

Rails 5: Access create method from another controller

I'm currently developing a client dashboard, there the client is able to see new offers, reservations and more.

The problem now is that I have a reservations controller and a dashboards controller

I want to display a pay button in my index.html.erb from the dashboards folder. For example:

<% @services.each do |service| %>

<%= form_for([@service, @service.reservations.new]) do |f| %>
<div class="col-12 col-sm-4">
  <%= f.submit "Bestellen", class: "btn btn-primary", style: 'float:right' %>
</div>
<% end %>

<% end %>

But then I get the following error:

ActionView::Template::Error (undefined method `reservations' for nil:NilClass):

This is my reservations controller:

  def create
    service = Service.find(params[:service_id])

if current_user == service.user
  flash[:alert] = "Du kannst nicht dein eigenes Angebot kaufen"
elsif current_user.stripe_id.blank?
  flash[:alert] = "Füge eine Zahlungsmehtode hinzu"
  return redirect_to payment_method_path
else
  @reservation = current_user.reservations.build(reservation_params)
  @reservation.service = service
  @reservation.price = service.price

  charge(service, @reservation)
end
redirect_to dashboard_path
  end

My dashboards controller

  def index
    @services = Service.all
  end

So I thought I would just create a file _form.html.erb in my reservations folder and then use <%= render 'reservations/form %> in my dashboards index, but this didn't work.

Upvotes: 0

Views: 43

Answers (1)

Mark Merritt
Mark Merritt

Reputation: 2695

You need to use the loop variable instead of the undefined instance variable...

<% @services.each do |service| %>
  <%= form_for [service, service.reservations.new] do |f| %>
    <div class="col-12 col-sm-4">
       <%= f.submit "Bestellen", class: "btn btn-primary", style: 'float:right' %>
    </div>
  <% end %>
<% end %>

Upvotes: 1

Related Questions