在视图和控制器之间传递参数 ruby 1.8.7

passing parameters between views and controllers ruby 1.8.7

我在视图中嵌套了这个表单 'A'

<div>
<%if current_user%> 
    <%=form_for(ModelB.new) do |f|%>
      <%if (params[:param_to_check].present?)%>
        <%f.hidden_field "param1",:value=> @modelA.id%>
        <%f.hidden_field "param2",:value=> current_user.id%>
      <%end%>
    render button
    <%end%>
<%else%>
    render other butotn
<%end%>

</div>

和Bs_controller中的这个'find':

@modelA=ModelA.find_by_id(params[:param1])
  :option_from_specific_gem=> -(@modelA.wanted_value).abs

当我查看传递给 Bs_controller 的参数时,我看到:

Parameters: {"authenticity_token"=>"some_hash=", "utf8"=>"✓", "y"=>"42", "x"=>"144"}

我需要将 hidden_field 的值传递给 Bs_controller。

谢谢大家的关注:)

这里是要点:https://gist.github.com/rmatuoka/dd6904809cf89cf916f7

param1param2 添加到您的允许参数列表中。在你的控制器中,你应该有这样的东西:

def modela_params
  params.require(:modela).permit(:x, :y, :param1, :param2)
end

我发表了一些评论,我会继续把它们变成一个答案。

有3种可能。假设这不是拼写错误,您当前不会将隐藏字段输出到 HTML。您需要我们<%= %>输出结果。

<%= f.hidden_field "param1",:value=> @modelA.id %>
<%= f.hidden_field "param2",:value=> current_user.id %>

这可能是问题所在。否则我猜 current_user 或 params[:param_to_check] 是 nil.