-1

i have created project scaffold and stage scaffold.i have a rendered a button on show page of the project to that open a form of stages. but i am getting no method error. i want to pass project_id to stage created. i have one to many association between project and stage. error

projects_controller.rb

  def index
    @projects = current_user.projects.all.paginate(page: params[:page], per_page: 15)
  end

  def show
    @project=Project.find(params[:id])
    @stages = Stage.all
  end

stages_controller.rb

  def index
    @stages = Stage.all
  end

  def show
  end

  def new
    @stages = Stage.new
    @project = Project.find(params[:project_id])
  end


  def create
    @project = Project.find(params[:project_id])
    @stage = @project.stages.build(stages_params)

    respond_to do |format|
      if @stage.save
        format.html { redirect_to @stage, notice: 'Stage was successfully created.' }
        format.json { render :show, status: :created, location: @stage }
      else
        format.html { render :new }
        format.json { render json: @stage.errors, status: :unprocessable_entity }
      end
    end

routes.rb

  resources :projects do
    resources :stages
  end

model project.rb

has_many :stages

rake routes

             Prefix Verb   URI Pattern                                                                              Controller#Action
                    tasks GET    /tasks(.:format)                                                                         tasks#index
                          POST   /tasks(.:format)                                                                         tasks#create
                 new_task GET    /tasks/new(.:format)                                                                     tasks#new
                edit_task GET    /tasks/:id/edit(.:format)                                                                tasks#edit
                     task GET    /tasks/:id(.:format)                                                                     tasks#show
                          PATCH  /tasks/:id(.:format)                                                                     tasks#update
                          PUT    /tasks/:id(.:format)                                                                     tasks#update
                          DELETE /tasks/:id(.:format)                                                                     tasks#destroy
           project_stages GET    /projects/:project_id/stages(.:format)                                                   stages#index
                          POST   /projects/:project_id/stages(.:format)                                                   stages#create
        new_project_stage GET    /projects/:project_id/stages/new(.:format)                                               stages#new
       edit_project_stage GET    /projects/:project_id/stages/:id/edit(.:format)                                          stages#edit
            project_stage GET    /projects/:project_id/stages/:id(.:format)                                               stages#show
                          PATCH  /projects/:project_id/stages/:id(.:format)                                               stages#update
                          PUT    /projects/:project_id/stages/:id(.:format)                                               stages#update
                          DELETE /projects/:project_id/stages/:id(.:format)                                               stages#destroy
                 projects GET    /projects(.:format)                                                                      projects#index
                          POST   /projects(.:format)                                                                      projects#create
              new_project GET    /projects/new(.:format)                                                                  projects#new
             edit_project GET    /projects/:id/edit(.:format)                                                             projects#edit
                  project GET    /projects/:id(.:format)                                                                  projects#show
                          PATCH  /projects/:id(.:format)                                                                  projects#update
                          PUT    /projects/:id(.:format)                                                                  projects#update
                          DELETE /projects/:id(.:format)                                                                  projects#destroy

project show.html.erb

<%= link_to "Add Stage", new_project_stage_url(@project), :class=>"button primary small" %>

updated error

Anuj
  • 12
  • 5

2 Answers2

0

I think you have an arror because in your new method you are setting a variable called @stages

def new
  @stages = Stage.new
  @project = Project.find(params[:project_id])
end

but in your view you are using a variable called @stage.

Just change @stages into @stage in your controller method #new and you might be good to go like so :

def new
  @stage = Stage.new
  @project = Project.find(params[:project_id])
end

And in you view :

<% if @stage.errors.any? %>
olivier dumas
  • 148
  • 2
  • 6
  • def new @stage = Stage.new @project = Project.find(params[:project_id]) end generates error- undefined method `stages_path' for #<#:0x00007f39800dbb30> – Anuj Jan 15 '20 at 11:08
  • edit: when you type `rails routes` in your console do you see a stages_path route ? I think you are trying to use a route that doesn't exist in your app – olivier dumas Jan 15 '20 at 11:12
  • i have added the routes in question – Anuj Jan 15 '20 at 11:23
0

You are using nested routes but in form_with you have not specified that. Try to change the form_with like this:

form_with(model: stage, url: [@project, stage])

Source: https://stackoverflow.com/a/46919792/4207394

You will have to check the variable names, i.e., the stage variable should have the new Stage object and @project variable should have the Project object or else please change the names accordingly.

Deepesh
  • 6,138
  • 1
  • 24
  • 41
  • hey now i can render the form but on submit i am not able to save data into table of stage into database. – Anuj Jan 16 '20 at 09:10
  • You can check the logs (terminal where you have ran the command `rails s`) to see what error you are getting while submitting the form. – Deepesh Jan 16 '20 at 13:26