P
P
pakrauce2016-05-01 19:57:25
Ruby on Rails
pakrauce, 2016-05-01 19:57:25

How to update a field in the database on RoR by clicking on a link?

Just started to poke to learn Ruby and RoR. In the index view, where all the data from the database table is displayed, I want to add a link to update one of the fields.

<tbody>
    <% @my_table.each do |my_table| %>
      <tr>
        <!-- -->
        <td><%= link_to 'Тут ссылка, которая должна обновить одно из полей в БД у текущей записи', ............, method: :patch, data: {confirm: 'Вы уверены?'} %>
      </tr>
    <% end %>
  </tbody>

What function to use in place of the ellipsis? What's in the controller? All you need to do is increment the number in my_table.my_field by one

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fahrenhe17, 2016-05-01
@pakrauce

Well look. Most likely you have an update action for my_table. It says something like
let's write in full for clarity:

@my_table.update_params(params.require(:my_table).permit(:my_field, :my_field2)

That is, when a patch request comes into this action, then attributes such as my_field and my_field2 are allowed to it.
Now the task is easier. Submit a patch request with the required attributes.
In our case, the request needs to be sent using link_to. Well, let's go to the off-line documentation, look for link_to .
We read everything carefully and find the line:
and below is the example code:
link_to "Nonsense search", searches_path(foo: "bar", baz: "quux")
# => <a href="/searches?foo=bar&amp;baz=quux">Nonsense search</a>

stay small! write
But in this way of solving there are small pitfalls, so it is better to attach ajax to it. It's easy too, all you need is some traffic and a great app - google.com

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question