簡體   English   中英

Rails 參數未傳遞給 controller 操作

[英]Rails params not passing to controller action

這是我的 controller:

class RegularController < ApplicationController
  before_filter :find_student

  def regular_student
    session[:page] = 'regular_student'
  end

  def generate_regular_student
    @regular = RegularStudent.new()
    @regular.codtitulo = params[:academic_title_id]

    render :template => 'regular/_regular_student_output'
  end
end

這是解決此問題的重要途徑:

get '/regular_student' => 'regular#regular_student'
post '/generate_regular_student' => 'regular#generate_regular_student'

這是我的regular_student.html.rb表格:

<%= form_for :generate_regular_student, url: "generate_regular_student" do |f| %>
  <%= f.collection_select :id, @student.academic_titles, :academid_title_id, :name, {}, {class: "form-control 
  screen-only"}  %>
  <%= f.submit "Generar constancia", {class: 'btn btn-default'} %>
<% end %>

這是我的部分_regular_student_output.html.erb ,我想在其中顯示作為@regular.codtitulo的參數的academic_title_id ,以確保我正確地獲取它:

<h1>Generated!</h1>
<h2>Title: <%= @regular.codtitulo %></h2>

問題是參數永遠不會到達 controller。當我按下表單中的提交按鈕時,終端輸出以下內容:

Started POST "/generate_regular_student" for 172.16.2.67 at 2022-04-26 09:00:44 -0300
Processing by RegularController#generate_regular_student as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"QjHc7k7NXlYgL1XlTiy6rYGgSvID+5II+YusNm7n+i3XT6vNwUzLUxmjG6aTEqAQP6zIKNsetAKpg2L3Qip4AA==", "generate_regular_student"=>{"academic_title_id"=>"221"}, "commit"=>"Generar constancia"}

因此參數生成正確,但由於某種原因從未達到 controller。當我在_regular_student_output.html.erb中使用@regular.codtitulo regular.codtitulo 時,我只得到一個空白<h2> </h2>

正如 dbugger 評論的那樣,參數達到了 controller。我的錯誤是在使用它時。 而不是使用:

params[:academic_title_id]

我應該使用:

params[:generate_regular_student][:academic_title_id]

學分調試器。 非常感謝你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM