Ruby on Rails : link_to 'update' and 'delete' doesn't work for me

Viewed 37

im newbie in RoR. I'm trying to build my own app. I keep getting error, even not error but nothing happens while I'm trying to delete or edit(update) a row. I created an Apprentice model with the values like; name, surname, grade. The application adds the values entered by me in form to the database. I display the entered data on the main page (root) in the table. I have a problem with deleting and editing these values. In the table I have placed two buttons - Delete and Edit and they display next to each row.

>    <td>
>         <!--buttons-->
>         <%= link_to "Update", home_update_path(student.id) %>
>         OR
>         <%= link_to 'Delete', root_path(student.id), :confirm => 'Are you sure?', :method => :delete %>
>       </td>

In the case of editing, I am redirected to the subpage, but the values that I want to display in the form do not appear. Only the id chosen by me displays in the address bar. For delete, nothing happens when pressed. Would someone be able to help me? My controller:

class HomeController < ApplicationController
  def index
    @all_students = Apprentice.all
  end

  def insert
    @name = params[:name]
    @surname = params[:surname]
    @grade = params[:grade]

    @insert = Apprentice.new
    if @name.present? && @surname.present? && @grade.present?
    Apprentice.create(name: @name, surname: @surname, grade: @grade)
    end
  end

  def update #the same here with edit
    @update =   Apprentice.find { params[:id]  }
  end

  def delete
    @delete =   Apprentice.find { params[:id]  }
  end

Ah and there is also my update view where i want to display selected values

<%= form_with model: @update, local: true do |f| %>
  <%= f.text_field :name %>
  <%= f.text_field :surname %>
  <%= f.text_field :grade %>
  <%= f.submit %>

<% end %>

routes:

> Rails.application.routes.draw do   root 'home#index'   get
> 'home/insert'   get 'home/update'   get 'home/update/:id' =>
> 'home#update'   get 'home/:id/delete', to: 'home#delete'   delete
> '/home/:id' => 'home#delete' end
1 Answers

There is just too much wrong here to address usefully, I would recommend starting with the excellent scaffold generators built into Rails, so you can see an example of working code for the standard operations. So from your command line:

rails generate scaffold student name:string

Then take a look at the controller, views, etc. that were created for you.

Related