对于我的最终项目,我需要创建两个带有提交按钮的单选按钮,该按钮将显示我所有课程和我专业课程的GPA。我可以轻松地显示它们,但是当我需要使用单选按钮只显示一个
时,它会卡住
<h2>Please select which GPA you would like to view:</h2>
<%= form_tag("/trans/transcript", :method => "get") do %>
<table>
<tr>
<th>Major Credits</th>
<th>All Credits</th>
</tr>
<tr>
<td><%= radio_button_tag(:gpa, "Major") %></td>
<td><%= radio_button_tag(:gpa, "All") %> </td>
</tr>
</table>
<%= submit_tag("View GPA") %>
<% end %>
<p> Major credits GPA <%= @transcript.GPA_for_major %>
<p> All credits GPA <%= @transcript.GPA_for_non_major %>
&#13;
一切看起来都不错,但我不知道如何设置控制器来说明他是否点击了主要的gpa单选按钮并点击了#34;查看GPA&#34;这&lt;%= @ transcript.GPA_for_major%&gt;应该显示
答案 0 :(得分:0)
假设控制器是trans并且动作是transcript,那么你应该在TransController中做什么
class TransController < ApplicationController
def transcript
if params[:gpa].nil?
@transcript = @gpa = nil
else
@gpa = params[:gpa]
@transcript = ... # Find your transcript here
end
end
# ... other actions
# ... other actions
# ... other actions
end
以下是trans / transcript.html.erb视图的外观 -
<h2>Please select which GPA you would like to view:</h2>
<%= form_tag('trans/transcript', :method => "get") do %>
<table>
<tr>
<th>Major Credits</th>
<th>All Credits</th>
</tr>
<tr>
<td>Major<%= radio_button_tag(:gpa, "Major") %></td>
<td>All<%= radio_button_tag(:gpa, "All") %> </td>
</tr>
</table>
<%= submit_tag("View GPA") %>
<% end %>
<% if @gpa == "Major" %>
<%= @transcript.GPA_for_major %>
<% elsif @gpa == "All" %>
<%= @transcript.GPA_for_major %>
<% end %>