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)

routes.rb


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

image.png

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.

index.html.erb


<%=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.

birds_controller.rb


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

And the edit screen.

edit.html.erb


<%=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"%>
<%end%>

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!

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!

birds_controller.rb


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

image.png

image.png

image.png

It has been properly changed to Kingfisher 2!

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

routes.rb


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!

routes.rb


  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!

index.html.erb


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

Then the controller receives the id and gets the record!

birds_controller.rb


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

Finally, show.html.erb!

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;
 ">
</div>

image.png

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] Implementation of drag and drop function (with effect)
Rails fuzzy search function implementation
[Rails] Implementation of category function
[Rails] strftime this and that
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[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] 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
[Ruby on rails] Implementation of like function
[Rails] Implementation of search function using gem's ransack
[Rails] Implementation of image enlargement function using lightbox2
[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