将参数从 html 发送到控制器中的方法
Send parameter from html to method in controller
我有 table 并且我在 html 中显示了这个 table 的内容。显示的每条记录都有按钮。我想在单击按钮时将记录的 ID 发送到我的控制器的方法。我做错了什么?
my_controller.rb
class MyController < ApplicationController
skip_authorization_check
def index
@contacts = Mytable.all
end
def add
byebug
ap params[:id]
end
end
index.html.slim
- debug(params) if Rails.env.development?
body
center
p
|
Contacts.
p.contact-list
- @contacts.each do |a|
.name
p.fname
= a['fname']
.surname
p.lname
= a['lname']
.phone
p.phone
= a['phone']
.rocknroll
p.rock
= link_to 'Add', method: add, class: 'icon', value: a['id ']
routes.rb
get 'my/' => 'my#index', as: 'my'
get 'my/add'
错误
NameError: undefined local variable or method `add'
NameError: undefined local variable or method `add'
我认为错误来自这一行
= link_to 'Add', method: add, class: 'icon', value: a['id ']
没有方法add
。可用的方法有 get
、post
、put
等
如果您尝试修改现有记录,则应使用 PATCH 方法。查看示例 here。
我认为应该是这样的:
= link_to 'Add', method: :patch, class: 'icon', value: a['id ']
并且在routes.rb
resources :contacts do #Assuming you have a Contact model
member do
patch 'add' #Or whatever you want to call your method.
end
end
我有 table 并且我在 html 中显示了这个 table 的内容。显示的每条记录都有按钮。我想在单击按钮时将记录的 ID 发送到我的控制器的方法。我做错了什么?
my_controller.rb
class MyController < ApplicationController
skip_authorization_check
def index
@contacts = Mytable.all
end
def add
byebug
ap params[:id]
end
end
index.html.slim
- debug(params) if Rails.env.development?
body
center
p
|
Contacts.
p.contact-list
- @contacts.each do |a|
.name
p.fname
= a['fname']
.surname
p.lname
= a['lname']
.phone
p.phone
= a['phone']
.rocknroll
p.rock
= link_to 'Add', method: add, class: 'icon', value: a['id ']
routes.rb
get 'my/' => 'my#index', as: 'my'
get 'my/add'
错误
NameError: undefined local variable or method `add'
NameError: undefined local variable or method `add'
我认为错误来自这一行
= link_to 'Add', method: add, class: 'icon', value: a['id ']
没有方法add
。可用的方法有 get
、post
、put
等
如果您尝试修改现有记录,则应使用 PATCH 方法。查看示例 here。
我认为应该是这样的:
= link_to 'Add', method: :patch, class: 'icon', value: a['id ']
并且在routes.rb
resources :contacts do #Assuming you have a Contact model
member do
patch 'add' #Or whatever you want to call your method.
end
end