我在mypage.html.erb
上有一个复选框列表。当我选中一个框并单击“提交”按钮时,我希望在下一页(bookfinal.html.erb
)上打印的内容的值。由于某种原因,检查的值不打印?在控制台中,选中的值显示为“1”,因此我不确定缺少什么。正如您所看到的,我添加了@checked = "Test"
以查看该值是否会打印,但它仍然没有?
mypage.html.erb
<div class="infoheaders">1. Question
<%= simple_form_for @category, :url => url_for(:action => 'bookfinal', :controller => 'users'), :method => 'post' do |f| %>
<%= f.collection_select(:title, Category.all, :title, :title) %>
<div class="grey-text-subheader">Device: (optional)</div>
<%= f.check_box :Apple %> <%= f.label "Apple" %>
<%= f.check_box :iPhone %> <%= f.label "iPhone" %>
<%= f.check_box :iPad %> <%= f.label "iPad" %>
<%= f.check_box :Mac %> <%= f.label "Mac" %>
<%= f.check_box :Android %> <%= f.label "Android" %><br><br>
<%= f.check_box :Samsung %> <%= f.label "Samsung" %>
<%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>
<%= f.check_box :Windows %> <%= f.label "Windows" %>
<%= f.check_box :Google %> <%= f.label "Google" %>
<%= f.button :submit, 'Submit' %>
<% end %>
</div>
users_controller.rb
def bookfinal
if params[:category][:ipad].present?
@checked = params[:category][:ipad]
end
if params[:category][:android].present?
@checked = params[:category][:android]
end
if params[:category][:samsung].present?
@checked = params[:category][:samsung]
end
@checked = "Test"
end
bookfinal.html.erb
<div>
The checked value is <%= @checked %>
</div>
的routes.rb
Rails.application.routes.draw do
get 'categories/new'
get 'sessions/new'
post 'categories' => 'categories#create'
get 'users/edit'
get 'users/new'
get 'users/index'
get 'users/mypage'
post "users/bookfinal" => 'users#bookfinal'
resources :users
resources :categories
控制台
Started POST "/users/bookfinal" for 96.49.105.234 at 2016-02-19 21:18:21 +0000
Processing by UsersController#bookfinal as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"zaPn5YuBFYr9VwPDSNMeA7tfm74sjM8HZneG+PCX9HEPEH/mb2qgA4xDacI9YDgcjDlFgKCQFFgWnUs5AZB0MA==", "category"=>{"title"=>"Digital Marketing", "Apple"=>"0", "iPhone"=>"0", "iPad"=>"1", "Mac"=>"0", "Android"=>"0", "Samsung"=>"0", "Microsoft"=>"0", "Windows"=>"0", "Google"=>"0"}, "commit"=>"Submit"}
Rendered users/bookfinal.html.erb within layouts/application (0.6ms)
Completed 200 OK in 115ms (Views: 113.8ms | ActiveRecord: 0.0ms)
答案 0 :(得分:1)
对于你目前的参数,你可以这样做:
@checked = params[:category].except(:title).select { |brand, value| value == '1' }.keys.to_sentence
这将从[:category]
获取参数,仅选择保留,然后从品牌名称中删除句子。
要处理未选择任何内容的情况,您可以使用:
@checked = 'None' if @checked.blank?
使用表单对象等有更清晰的解决方案。但这也应该起作用
答案 1 :(得分:1)
将您的bookfinal
方法代码更改为以下内容:
@checked = params[:category].select{|k,v| !v.to_i.zero?}.keys.join
答案 2 :(得分:0)
打印值是什么意思?
如果您从{1}}获得0
或1
,则所有内容都按预期工作checkboxes rendered from the form helper return either 0 or 1 if they are unchecked or checked respectively。
你无法获取复选框标签文字。请求发送的是html params[:category][:ipad]
元素的值,rails实际上将其绑定到隐藏字段(包含0或1),原因在我发布的上述链接中有所解释。
所以基本上你有两个选择:
我选择2号选项,所以你会做这样的事情:
在您的控制器中:
input
(您也可以将值存储在flash storage)
然后在def bookfinal
if params[:category][:ipad]
@ipad_checked = true
end
end
视图中
bookfinal.html.erb
答案 3 :(得分:0)
在这里,从params hash我们看到,ipad的选中值是'1',键是iPad而不是ipad,请将控制器改为此。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<LinearLayout
android:id="@+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:weightSum="3" >
<Button
android:id="@+id/bone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#3f51b5"
android:text="Q1" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/btwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#3f51b5"
android:text="Q2" />
</LinearLayout>
<LinearLayout
android:id="@+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/abc_ab_share_pack_mtrl_alpha" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<RatingBar
android:id="@+id/rtwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="3"
android:numStars="3"
android:progressTint="#FFD700"
android:stepSize="3"
android:layout_weight="1"/>
</LinearLayout>
<!-- second -->
<LinearLayout
android:id="@+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3" >
<Button
android:id="@+id/bthree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#3f51b5"
android:text="Q3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/bfour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#3f51b5"
android:text="Q4" />
</LinearLayout>
<LinearLayout
android:id="@+id/four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2" >
<RatingBar
android:id="@+id/rthree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="3"
android:numStars="3"
android:progressTint="#FFD700"
android:stepSize="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
/>
<RatingBar
android:id="@+id/rfour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="3"
android:numStars="3"
android:progressTint="#FFD700"
android:stepSize="3" />
</LinearLayout>
<!-- third -->
<LinearLayout
android:id="@+id/five"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3" >
<Button
android:id="@+id/bfive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#3f51b5"
android:text="Q5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/bsix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#3f51b5"
android:text="Q6" />
</LinearLayout>
<LinearLayout
android:id="@+id/six"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2" >
<RatingBar
android:id="@+id/rfive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="3"
android:numStars="3"
android:progressTint="#FFD700"
android:stepSize="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
/>
<RatingBar
android:id="@+id/rsix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="3"
android:numStars="3"
android:progressTint="#FFD700"
android:stepSize="3" />
</LinearLayout>
<!-- four -->
<LinearLayout
android:id="@+id/seven"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3" >
<Button
android:id="@+id/bseven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#3f51b5"
android:text="Q7" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/beight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#3f51b5"
android:text="Q8" />
</LinearLayout>
<LinearLayout
android:id="@+id/eight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2" >
<RatingBar
android:id="@+id/rseven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="3"
android:numStars="3"
android:progressTint="#FFD700"
android:stepSize="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
/>
<RatingBar
android:id="@+id/reight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="3"
android:numStars="3"
android:progressTint="#FFD700"
android:stepSize="3" />
</LinearLayout>
<!-- five -->
<LinearLayout
android:id="@+id/nine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3" >
<Button
android:id="@+id/bnine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#3f51b5"
android:text="Q9" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/bten"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#3f51b5"
android:text="Q10" />
</LinearLayout>
<LinearLayout
android:id="@+id/ten"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2" >
<RatingBar
android:id="@+id/rnine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="3"
android:numStars="3"
android:progressTint="#FFD700"
android:stepSize="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
/>
<RatingBar
android:id="@+id/rten"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="3"
android:numStars="3"
android:progressTint="#FFD700"
android:stepSize="3" />
</LinearLayout>
</LinearLayout>
您的bookfinal.html.erb是,
def bookfinal
if params['category']['iPad'].present?
@checked = 'Ipad is checked'
end
if params['category']['Apple'].present?
@checked = 'Apple is checked'
end
if params['category']['Samsung'].present?
@checked = 'Samsung is checked'
end
if params['category']['Microsoft'].present?
@checked = 'Microsoft is checked'
end
if params['category']['Android'].present?
@checked = 'Android is checked'
end
if params['category']['Mac'].present?
@checked = 'Mac is checked'
end
if params['category']['iPhone'].present?
@checked = 'iPhone is checked'
end
if params['category']['Windows'].present?
@checked = 'Windows is checked'
end
if params['category']['Google'].present?
@checked = 'Google is checked'
end
@checked = "Test"
end
多数民众赞成。完成: - )
这是你的params hash和@category对象,
<div>
<%= @checked %>
</div>