How to access the parameters in the mail request

Scenario: the user (class:) Userwants to apply to the course (class:) Courseonline by creating an application (class Application).

They visit the application page, in /applications/:id, where id is the course identifier. This is the controller:

  def new
    @course = Course.find_by_id(params[:id])
    @application = Application.new
  end

  def create
    @application = Application.new(application_params)

    @course = Course.find_by_id(params[:id])

    @application.course_id = @course.id
    @application.save
  end

This line does not work

@course = Course.find_by_id(params[:id])

because in the method that processes the POST request, you cannot access the parameters, but I require that they set course_id in the application.

0
source share
2 answers

The new_whatever_path link will only pass the identifier if the resource is nested. So I think you routes should look something like this:

resources :course do
    resources :application, only: [:new, :create]
end

_to ":, new_course_application_path (@course) ..

+2

, ActiveRecord, .

def create
   @course = Course.find(params[:id]) #if its nested and associated then (params[:course_id])
   @application = @course.applications.new(application_params)
   @application.save
end
+1

Source: https://habr.com/ru/post/1614097/


All Articles