I have the following form:
<%= form_for :user, :url => { :action => :update, :controller => :usuario, :id => current_user.id }, :method => :put do |f| %>
Nueva encuesta:
<div style="padding-bottom: 11px;">
<div class="field">
<%= f.label :servicio %><br />
<%= f.check_box :fluido, {}, "fluido", false%>Servicio de fluidos<br />
<%= f.check_box :solido, {}, "solido", false %>Servicio de control de sólidos<br />
<%= f.check_box :ambiente, {}, "ambiente", false %>Servicio Ambiental<br />
</div>
<button type="button" class="btn btn-primary">
<%= f.submit %>
</button>
</div>
and I want to validate the value of the checkboxes to perform certain actions inside the controller, for example:
user_controller.rb:
@usuario = User.find(params[:id])
if (request[:fluido]=='fluido')
if @usuario.update_attributes(:fluido => 'fluido')
redirect_to 'index'
end
end
if (request[:solido]=='solido')
if @usuario.update_attributes(:solido => 'solido')
redirect_to 'index'
end
end
if (request[:ambiente]=='ambiente')
if @usuario.update_attributes(:ambiente => 'ambiente')
redirect_to 'index'
end
end
the problem is that the values of the request object that are being compared in the if are 'nil', that is, they are not returning the form values
The correct way to call parameters is with
params
, notrequest
, so your code should look something like:Separate comments:
if
you have, it will not be updated when calling submit. In general, I think that you would simplify your code much more if you used strong parameters , where you would define which parameters are allowed or not, in addition to solving that potential bug that I mentioned.fluido
denotes whether it is fluent or not, the same withsolido
, etc. Also that way you wouldn't need to redefine the true/false values of your checkbox: