Question: Problems displaying a list of my model instances

Question

Problems displaying a list of my model instances

Answers 0
Added at 2016-12-30 20:12
Tags
Question

in the application I have made, I have clients and projects. a Client has many projects and a project belongs to a client. Projects are made by first creating a client, and each project object is created via a client.

After I create a project object and I hit save, i get this error:

NoMethodError in ProjectsController#show

undefined method `set_project' for # Did you mean? edit_project_url

the following is my project controller:

class ProjectsController < ApplicationController
  before_action :set_project, only: [:show, :edit, :update, :destroy]
  before_action :set_client, only: [:new, :create] 

  # GET /projects
  # GET /projects.json
  def index
    @projects = Project.all
  end

  # GET /projects/1
  # GET /projects/1.json
  def show
  end

  # GET /projects/new
  def new
    @project = @client.projects.new
  end

  # GET /projects/1/edit
  def edit
  end

  # POST /projects
  # POST /projects.json
  def create
    #@project = Project.new(project_params)
    @project = @client.projects.new(project_params)

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

  # PATCH/PUT /projects/1
  # PATCH/PUT /projects/1.json
  def update
    respond_to do |format|
      if @project.update(project_params)
        format.html { redirect_to @project, notice: 'Project was successfully updated.' }
        format.json { render :show, status: :ok, location: @project }
      else
        format.html { render :edit }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /projects/1
  # DELETE /projects/1.json
  def destroy
    @project.destroy
    respond_to do |format|
      format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_client
      #@project = Project.find(params[:id])

      @client = Client.find_by(id: params[:client_id]) || Client.find(project_params[:client_id]) 
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def project_params
      params.require(:project).permit(:client_id, :project_description, :project_timescale)
    end
end

This is my client controller:

class ClientsController < ApplicationController
  before_action :set_client, only: [:show, :edit, :update, :destroy]

  # GET /clients
  # GET /clients.json
  def index
    @clients = Client.all
  end

  # GET /clients/1
  # GET /clients/1.json
  def show
  end

  # GET /clients/new
  def new
    @client = Client.new
  end

  # GET /clients/1/edit
  def edit
  end

  # POST /clients
  # POST /clients.json
  def create
    @client = Client.new(client_params)

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

  # PATCH/PUT /clients/1
  # PATCH/PUT /clients/1.json
  def update
    respond_to do |format|
      if @client.update(client_params)
        format.html { redirect_to @client, notice: 'Client was successfully updated.' }
        format.json { render :show, status: :ok, location: @client }
      else
        format.html { render :edit }
        format.json { render json: @client.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /clients/1
  # DELETE /clients/1.json
  def destroy
    @client.destroy
    respond_to do |format|
      format.html { redirect_to clients_url, notice: 'Client was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_client
      @client = Client.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def client_params
      params.require(:client).permit(:name, :email)
    end
end

the following are the view assosciated with the project model:

the form:

<%= form_for @project do |f| %>
  <% if @project.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= "#{pluralize(@project.errors.count, "error")} prohibited this project from being saved:" %>
      </h2>
      <ul>
        <% @project.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.hidden_field :client_id, value: @project.client.id  %>
  <div class="field">
    <%= f.label :client %>
      <p>
        <%= @project.client.name %>
      </p>
  </div>
  <div class="field">
    <%= f.label :project_description %>
    <%= f.text_area :project_description %>
  </div>
  <div class="field">
    <%= f.label :project_timescale %>
    <%= f.text_field :project_timescale %>
  </div>
  <div class="actions">
    <%= f.submit 'Save' %>
  </div>
<% end %>

Edit:

%h1 Editing project

= render 'project_form'

= link_to 'Show', @project
\|
= link_to 'Back', projects_path

Index:

%h1 Listing projects

%table
  %thead
    %tr
      %th Client
      %th Project description
      %th Project timescale
      %th
      %th
      %th

  %tbody
    - @projects.each do |project|
      %tr
        %td= project.client.name
        %td= project.project_description
        %td= project.project_timescale
        %td= link_to 'Show', project
        %td= link_to 'Edit', edit_project_path(project)
        %td= link_to 'Destroy', project, :method => :delete, :data => { :confirm => 'Are you sure?' }

%br

= link_to 'New Project', new_project_path

New:

%h1 New project

= render 'project_form'

= link_to 'Back', projects_path

Show:

%p#notice= notice

%p
  %b Client:
  = @project.client.name
%p
  %b Project description:
  = @project.project_description
%p
  %b Project timescale:
  = @project.project_timescale

= link_to 'Edit', edit_project_path(@project)
\|
= link_to 'Back', projects_path
Answers to

Problems displaying a list of my model instances

Source Show
◀ Wstecz