gpt4 book ai didi

ruby-on-rails - 找不到 'id' =clas 的时间表

转载 作者:数据小太阳 更新时间:2023-10-29 08:03:39 24 4
gpt4 key购买 nike

生成了一个时间表脚手架。然后在我的 timetables_controller.rb 中创建了一个新的 Action “clas”。

timetables_controller.rb

class TimetablesController < ApplicationController
before_action :set_timetable, only: [:show, :edit, :update, :destroy, :clas]
def clas
@classtimetable = Timetable.where(params[:clas])
end

//other actions

def set_timetable
@timetable = Timetable.find(params[:id])
end

def timetable_params
params.require(:timetable).permit(:day, :clas, :one_teacher, :one_sub, :two_teacher, :two_sub, :three_teacher, :three_sub, :four_teacher, :four_sub, :five_teacher, :five_sub, :six_teacher, :six_sub, :seven_teacher, :seven_sub, :eight_teacher, :eight_sub)
end
end

为 Action “clas”创建了 form_for。需要将选择选项值作为参数传递给 Controller ​​

clas.html.erb

<% form_for @classtimetable, :url => clas_timetables_path, :html => { :method => :post} do |f| %>
<div class="field">
<%= f.label "Select class" %>
<% values = ["1c", "2c", "3c", "4d", "5i"] %>
<%= f.select :clas, options_for_select(values) %>
</div>
<div class="actions">
<%= f.submit "Submit"%>
</div>
<% end %>

<% @classtimetable.each do |class_timetable| %>
<%= class_timetable.day %>
<% end %>

routes.rb

resources :timetables do
member do
get 'clas'
end
end

我需要在我的 clas.html.erb 页面上全天上课。这是通过从下拉列表中选择类并提交来完成的。当我点击提交时,该值应该在参数中传递。不知道如何解决它。有什么想法吗?

最佳答案

form_for标签中添加 Controller ,

<% form_for @classtimetable, :url => { :controller => "your-controller-name", :action => :clas } do |f| %>

或者你可以直接这样写,

<% form_for @classtimetable, :url => clas_timetables_path, :html => { :method => :post} do |f| %>

改变 before_action 就像,

before_action :set_timetable, only: [:show, :edit, :update, :destroy, :clas]

您可以将 collection 更改为 member 并传递 id

resources :timetables do
member do
get 'clas'
end
end

关于ruby-on-rails - 找不到 'id' =clas 的时间表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30634993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com