If you do not have a route, and you simply pass the id through get, like in php, then the approach is incorrect. You must add a route to /config/routes.rb
get "/plans/:project_id", to: 'plans#index', as: 'plans_project'
The as parameter specifies the name of your route. By this name you can always call it.
In your PlansController, in the create method there should be an entry, like:
def create @plan = @project.plans.new(plan_params) respond_to do |format| if @plan.save format.html { redirect_to plans_project_path(@project), notice: 'Plan was successfully created.' } format.json { render action: 'show', status: :created, location: plans_project_url(@project) } else format.html { render action: 'new' } format.json { render json: @plan.errors, status: :unprocessable_entity } end end end
And in the new method:
@project = Project.find(params[:project_id]) @plan = @project.plans.new
Change redirect_to in format.htm and the location parameter render in format.json to plans_project_path and plans_project_url . As a parameter, pass them the object of the parent project, as shown in the example above.
And your index is incorrect:
@project_id = params[:project_id] @plan = Plan.where("project_id == ?", @project_id)
It should be replaced by:
@project = Project.find(params[:project_id]) # Получаем объект проекта по id. @plans = @project.plans # Получаем все планы связанные с данным проектом.
To make it work, make sure that the connections are spelled out in the models. In plan.rb:
belongs_to :project
And in project.rb:
has_many :plans