Rails CRUD function implementation ② (edited and detailed this time)

Continuing from the previous , I will describe the edits and details. Procedure when adding a function Review: routes.rb → Add controller → Add view

Editing function The procedure is similar to a new post. Edit screen display (edit) → Update with input contents (update)


resources :birds, only: [:index, :new, :create, :destroy, :edit]


Paste the link to the edit screen on the list screen. Prefix passes the id with "edit_bird" and it is a GET method, so it will be as follows.


<%=link_to "Edit", edit_bird_path(bird.id), method: :get %>

Get the record that matches the passed id and pass it to the edit screen.


def edit
  @bird = Bird.find(params[:id])

And the edit screen.


<%=form_with(model:@bird, local:true) do |form|%>
  <%=form.text_field :name, placeholder: "Bird name"%>
  <%=form.text_field :pic, placeholder: "Bird photo URL"%>
  <%= form.submit "update"%>

The link to the edit screen is displayed properly. image.png

If you click it, it will transition properly! image.png

Update process from here! First of all, routes.rb!


resources :birds, only: [:index, :new, :create, :destroy, :edit, :update]

The controller receives the input contents and updates the DB! Use bird_param created in the new post!


  def update
    bird = Bird.find(params[:id])
  def bird_param
    # params.require(:Model name).permit(:Column name,:Column name,......)
    params.require(:bird).permit(:name, :pic)




It has been properly changed to Kingfisher 2!

Detailed display function It's finally the last feature! let's do our best! !!


resources :birds, only: [:index, :new, :create, :destroy, :edit, :update, :show]

It's okay, but since all the functions have been added, you don't need to write only! Let's erase it!


  resources :birds

It was pretty refreshing! !!

Paste the link to the details screen on the list screen! When you run the rails routes command image.png The prefix is bird, the method is GET, and let's do the id too!


<%= link_to 'Details', bird_path(bird.id), method: :get %>

Then the controller receives the id and gets the record!


def show
    @bird = Bird.find(params[:id])

Finally, show.html.erb!


<%= @bird.name %>
<div style=
"background-image: url(<%= @bird.pic %>); 
 background-position: center center;
 background-size: cover;
 width: 300px; 
 height: 300px;
 margin-bottom: 10px;


The one you have selected is displayed.

Summary Rails will automatically identify the link destination if you pass a model, and if you understand the rules of the framework, you can create a basic site at explosive speed. This time, we will implement the login function, so please look forward to it! Thank you until the end! !!

Recommended Posts

Rails CRUD function implementation ② (edited and detailed this time)
Rails CRUD function implementation ① (new addition, deletion this time)
Rails Basic CRUD function implementation procedure scaffold
Rails search function implementation
[Rails] Implementation of drag and drop function (with effect)
Rails fuzzy search function implementation
[Rails 6] Implementation of search function
[Rails] Implementation of category function
Login function implementation with rails
[Rails] strftime this and that
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails 6] Pagination function implementation (kaminari)
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[Rails] Save start time and end time
[Rails] Implementation of image preview function
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Rails] [jQuery] Asynchronous like function implementation using remote: true and js.erb
CRUD features and MVC in Rails
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
[Rails 6] Like function (synchronous → asynchronous) implementation
[Rails] Comment function implementation procedure memo
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
Rails Addition of easy and easy login function
[Ruby on Rails] Follow function implementation: Bidirectional
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Rails] Implementation of search function using gem's ransack
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
[Rails] Implementation of retweet function in SNS application
[Nuxt / Rails] POST implementation using axios and devise_token_auth
Ruby on Rails Email automatic sending function implementation
This and that of conditional branching of rails development