DDDD
DDDD

Reputation: 3940

Routing a resource to controller

How do you route a resource to its controller? I am using the resource in an edit page for a different model, so my actions are being routed to its model controller first.

This edit page requests from

class Grandstreamers::ResellersController < ApplicationController
def new
end etc...

I am trying to route the requests to here instead:

Grandstreamers::CertificatesController < ApplicationController
def new
end
def update
end etc...

This is my form under views/grandstreamers/resellers/edit.html.erb

<%= form_for @untrained, :url => certificates_update_path(@untrained) do |f| %>
            <p> Trained Users </p>
            <%= select_tag "certificate[user_id]", options_for_select(@current_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
            <%= f.submit "Un-Train", class: "btn btn-large btn-primary" %>
        <% end %>

        <%= form_for @trained, :url => certificates_create_path(@trained) do |f| %>
            <p> Non-Trained Users </p>
            <%= select_tag "certificate[user_id]", options_for_select(@non_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
            <%= f.submit "Train", class: "btn btn-large btn-primary" %>
        <% end %>

My route is:

resources :certificates

Note that the

:url => certificates_create_path

is not correct and not working. Is there a way to specify this route in my routes.rb file or in my form? Thank you

EDIT

This is my resellers_controller edit() which is routes to first.

@trained = Certificate.new(params[:certificate])

  #Trying to get to certificates_controller update. Then update the :attend to "No"
  #@untrained = Certificate.new(params[:certificate])
  #@untrained = Certificate.find(params[:id])
  #@untrained = Certificate.find_by_user_id(params[:id])

@untrained is not defined, I am not sure how to get it to just go to my certificate controller. For @trained I can define it since its not made yet and does not give me errors when it cant find a correct value.

My certificates controller which uses create() but cannot get to update()

def create

@trained = Certificate.new(params[:certificate])
if @trained.save
  @trained.update_attributes(attend: "Yes")
end
redirect_to grandstreamers_resellers_path

end

def update
@untrained = Certificate.find(params[:id])
@untrained.update_attributes(attend: "No")
redirect_to grandstreamers_resellers_path
end

Major Issue The instance variable @trained and @untrained need to be defined somehow in reseller_controller. What can I define them as to load the edit page?

Part Solution

I defined this is in my resellers_controller and it loads the edit page now.

@untrained = User.find(params[:id])

Now I get this error:

No route matches [PUT] "/certificates.1"

Upvotes: 0

Views: 120

Answers (2)

Jerry Clinesmith
Jerry Clinesmith

Reputation: 424

I believe the problem is you need to let routing know the full name to the controller.

From the Rails routing guide:

scope module: 'Grandstreamers' do
  resources :certificates
end

Use these paths when creating the form:

<%= form_for @untrained, :url => certificate_path(@untrained), :method => :put do |f| %>

<%= form_for @trained, :url => certificates_path, :method => :post do |f| %>

Upvotes: 1

HarsHarI
HarsHarI

Reputation: 911

use this his routes.rb file

namespace :grandstreamers do
 resources :certificates
end

Upvotes: 0

Related Questions