保存返回true,对象未保存。我无法解释原因。我怀疑我需要明确告诉rails它是可修改的,但我不知道如何。知道什么可能是错的吗?
<%= form_tag(set_email_settings_path) do %>
<div class="form-group">
<% if @logged_in_profile.email_settings != 'off' %>
<%= submit_tag 'Turn off emails', class: 'btn btn-default btn-about', name: 'email_notification' %>
<% else %>
<%= submit_tag 'Turn on emails', class: 'btn btn-default btn-about', name: 'email_notification' %>
<% end %>
</div>
<% end %>
if params[:email_notification] == 'Turn off emails'
logged_in_profile.email_settings = 'off'
else
logged_in_profile.email_settings = 'normal'
end
if logged_in_profile.save
redirect_to email_settings_path
else
redirect_to settings_path
end
post "set_email_settings" => "profiles#set_email_settings", :as => 'set_email_settings'
答案 0 :(得分:1)
请尝试将其作为表单,因为我认为您不会在表单中提交任何值...
<%= form_tag(set_email_settings_path) do %>
<div class="form-group">
<% if @logged_in_profile.email_settings == 'normal' %>
<%= hidden_field_tag 'email_notification', 'Turn off emails' %>
<%= submit_tag 'Turn off emails', class: 'btn btn-default btn-about' %>
<% else %>
<%= hidden_field_tag 'email_notification', 'Turn on emails' %>
<%= submit_tag 'Turn on emails', class: 'btn btn-default btn-about' %>
<% end %>
</div>
<% end %>
你会在上面看到,我改掉了'!=';只是避免'!='的好习惯。如果没有'else','除非'也可以使用<% unless @logged_in_profile.email_settings == 'off' %>
。