1

I'd like to update the data without form.

Although there are similar questions, they don't work for me.

Update field through link_to in Rails

link_to update (without form)

What I'd like to do is to delete data as followings;

For example, delete name and address when delete link is clicked.

id | name | address | ...
12 | aaa  | bbb     | ...

to

id | name | address | ...
12 |      |         | ...

Although I tried some, error was displayed.(such as ActionController::RoutingError)

schema

  create_table "rooms", force: :cascade do |t|
    t.string   "name"
    t.text     "address"
    ...

model

schedule.rb

class Schedule < ActiveRecord::Base
  belongs_to :user
  has_many :rooms, inverse_of: :schedule, dependent: :destroy
  accepts_nested_attributes_for :rooms, allow_destroy: true
  ...

room.rb

class Room < ActiveRecord::Base
  belongs_to :schedule, inverse_of: :rooms
  default_scope -> { order(day: :asc) }
  ...

view

I'd like to add the link in schedules/_schedule.html.erb It has the followings;

  ...
  <% schedule.rooms.each do |room| %>
  ...
    <% if room.name.present? %>
        <%= link_to "Delete", rooms_path(room, room:{address: nil}) , method: :put, data: { confirm: "You sure?"} %>
  ...

I also tried another code as below, but they don't work.

   <%= link_to "Delete", rooms_path(room:{address: nil}) , method: :put, data: { confirm: "You sure?"} %>

   <%= link_to "Delete", rooms_path(room) , method: :put, params: {address: nil}, data: { confirm: "You sure?"} %>

and so on.

routes.rb

...
  resources :schedules do
    resources :events
  end

  resources :schedules do
    resources :rooms
  end

  resources :rooms do
    resources :events
  end
...

rooms_controller.rb

...

def edit
  #nothing
end

def update
  @room = Room.find(params[:id])
  if @room.update(room_params)
    flash[:success] = "Room updated!"
    redirect_to itinerary_path(@room.itinerary) || current_user
  else
    render 'edit'
  end
end

def destroy
  @room = Room.find(params[:id])
  @room.destroy
  flash[:success] = "Room deleted"
  redirect_to schedule_path(@room.schedule) || current_user
end

private

  def room_params
    params.require(:room).permit(
      :id, :_destroy, :name, :address, :schedule_id, :day)
  end

...

It would be appreciated if you could give me any suggestion.

Community
  • 1
  • 1
SamuraiBlue
  • 851
  • 2
  • 17
  • 44

1 Answers1

3

You need to change rooms_path to room_path.

Without params:

<%= link_to "Delete", room_path(room) , method: :put, data: { confirm: "You sure?"} %>

With params:

<%= link_to "Delete", room_path(room, room: {name: nil, address: nil}) , method: :put, data: { confirm: "You sure?"} %>
Community
  • 1
  • 1
Gokul
  • 3,101
  • 4
  • 27
  • 45
  • Thank you for your answer, @Gokul M. Although I tried your code, another error is displayed. `ActionController::ParameterMissing (param is missing or the value is empty: room):`. – SamuraiBlue Jun 27 '16 at 13:10
  • @SamuraiBlue I changed my answer you can just pass the `room` to `room_path` – Gokul Jun 27 '16 at 13:13
  • Thank you for your update, @Gokul M. Although I tried both with and without params, they don't work. `ActionController::ParameterMissing (param is missing or the value is empty: room):` is displayed. – SamuraiBlue Jun 27 '16 at 14:17
  • Thank you for your comment, @Gokul M. I added the controller at the bottom of my question. – SamuraiBlue Jun 28 '16 at 11:04
  • Where is the delete action? – Gokul Jun 28 '16 at 12:32
  • Thank you for your comment, @Gokul M. I added the `destroy` in my controller. But I'm sorry if I mislead you. What I'd like to do is to erase the data in some columns such as `name` and `address`, although the link name is `Delete`. I don't want to delete the row. It's more to the edit than to the delete. – SamuraiBlue Jun 28 '16 at 13:05
  • change `@Room.destroy` to `@room.destroy`If you want to just update the fields you can just link_to your update path with `name` and `address` params as `room: {name: null, address: null}` – Gokul Jun 28 '16 at 13:12
  • Thank you for your comment, @Gokul M. I tried `<%= link_to "Delete", room_path(room: {name: null, address: null}), method: :put, data: { confirm: "You sure?"} %>`, the following error is displayed `NameError (undefined local variable or method 'null' for #<#):` It would be appreciated if you could show me how to apply to my code. – SamuraiBlue Jun 28 '16 at 13:30
  • Thank you for your comment, @Gokul M. It seems to work because the above error is not displayed. Although another error is displayed, it may be a different problem. Thank you for your time! – SamuraiBlue Jun 29 '16 at 12:12