如何从隐藏字段传递参数?

时间:2015-08-27 11:13:28

标签: ruby-on-rails ruby-on-rails-4

请帮助解决问题。

从视图im发送表单通过POST方法。

显示视图:

.........
........
<%= form_tag(user_change_poll_state_path(@user), method: :post, id: 'state_poll_form') do %> 
  <%= hidden_field(:act, :to_close) %>
  <%= hidden_field(:user_id, @user.id) %>
  <button type="submit" class="" id="statePollSubmit">Закрыть</button>
<% end %>
.........
..............

控制器:

class PollsController < ApplicationController
  def show    
    @user = User.find(params[:user_id])
    @images = @poll.images.paginate(page: params[:page], :per_page => 10)
  end

  def change_poll_state
    p '000000000000000000000000000000000000000000000000000'
    p state_poll_params[:act]
    p state_poll_params[:user_id]

    render 'show'
  end

  private
    def state_poll_params
      params.permit(:act, :user_id)
    end            
end

提交表单控制台后显示如下:

Started POST "/users/3/change_poll_state" for 127.0.0.1 at 2015-08-27 14:00:13 +0300
Processing by PollsController#change_poll_state as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"H2dFsSe9MDnCC2qLqT+GbltjvqIrC84Nf/h1Hnx5JN4WH/qGCRbIcsJi9kxe9qw+Fk9kHMvkhEkhxVestOnFVQ==", "act"=>{"to_close"=>""}, "user_id"=>"3"}
"000000000000000000000000000000000000000000000000000"
Unpermitted parameters: utf8, authenticity_token, act
nil
Unpermitted parameters: utf8, authenticity_token, act
"3"
Unpermitted parameters: utf8, authenticity_token, act
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)

请帮助传递:act和:user_id值到行动&#39; change_poll_state&#39;

1 个答案:

答案 0 :(得分:-1)

尝试更改

<%= hidden_field(:act, :to_close) %>

<%= hidden_field_tag(:act, :to_close) %>

然后在你的控制器中,

p state_poll_params[:act]
# => 'to_close'
p state_poll_params[:act].to_sym
# => :to_close