【发布时间】:2015-12-09 12:32:36
【问题描述】:
我是 Ruby(和 Rails)的新手,所以对这个基本问题表示歉意。
据我了解,控制器内部的params[...] 变量应该保存我刚刚在网页上提交的表单中的数据?我正在尝试使用以下代码根据字段是否为空白来更新模型:
@s3_destination = S3Destination.find(params[:id])
if params[:credentials_secret].to_s.empty?
update = @s3_destination.update_attributes({
:name => params[:name],
:bucket => params[:bucket],
:region => params[:region],
:credentials_id => params[:credentials_id]
})
if update
redirect_to @s3_destination
else
render 'edit'
end
else
update = @s3_destination.update_attributes({
:name => params[:name],
:bucket => params[:bucket],
:region => params[:region],
:credentials_id => params[:credentials_id],
:credentials_secret => params[:credentials_secret],
})
if update
redirect_to @s3_destination
else
render 'edit'
end
end
控制器动作执行良好,但模型中现在没有数据,所以我假设我在 update_attributes 函数中设置它时做错了。
params[:name] 不应该从名为“name”的表单元素中获取值吗?还是我误解了它的目的?
编辑
回答一些问题:
我相信这是相关的日志条目,对我来说一切正常吗?
2015 年 12 月 9 日 12:40:51 +0000 为 127.0.0.1 启动 PATCH "/s3_destinations/1" 由 S3DestinationsController#update 处理为 HTML 参数:{"utf8"=>"✓", "authenticity_token"=>"a26uTf4+58v/O+pTqOYn0R7cjzGVWBnbFVdJ1OWwNnybEpUdYt2FNruOla/ikOnAFLpY3mn71Wow5oVee+R3Ng==", "s3_destination"=>{"name,=>"Cogative Bucket 1" “bucket”=>“cogative-backup-01”,“region”=>“eu-west-1”,“credentials_id”=>“AKIAIZG2HJ35NR3DQYGA”,“credentials_secret”=>“12345”},“commit”=> "保存 S3 目的地", "id"=>"1"} 用户负载 (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] S3Destination 负载 (0.0ms) SELECT "s3_destinations".* FROM "s3_destinations" WHERE "s3_destinations"."id" = ?限制 1 [["id", 1]] (0.1ms) 开始交易 (0.0ms) 提交事务 重定向到 http://localhost:3000/s3_destinations/1 Completed 302 Found in 3ms (ActiveRecord: 0.2ms)表单本身是这样生成的:
<h2>Editing AWS S3 Destination</h2>
<%= form_for :s3_destination, url: s3_destination_path(@s3_destination), method: :patch do |f| %>
<% if @s3_destination.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@s3_destination.errors.count, "error") %> prohibited this source from being saved:
</h2>
<ul>
<% @s3_destination.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<%end %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :bucket %><br>
<%= f.text_field :bucket %>
</p>
<select name="s3_destination[region]">
<option value="us-east-1">US Standard (N. Virginia)</option>
<option value="us-west-2">US West (Oregon)</option>
<option value="us-west-1">US West (N. California)</option>
<option value="eu-west-1">EU (Ireland)</option>
<option value="eu-central-1">EU (Frankfurt)</option>
<option value="ap-southeast-1">Asia Pacific (Singapore)</option>
<option value="ap-southeast-2">Asia Pacific (Sydney)</option>
<option value="ap-northeast-1">Asia Pacific (Tokyo)</option>
<option value="sa-east-1">South America (Sao Paulo)</option>
</select>
<p>
<%= f.label :credentials_id %><br>
<%= f.text_field :credentials_id %>
</p>
<p>
<%= f.label :credentials_secret %><br>
<%= f.password_field :credentials_secret %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
【问题讨论】:
-
您理解正确,但请根据您的要求显示日志中关于
Parameters: {...}的内容 -
您使用的是哪个 Rails 版本?
-
旁注:使用
update_attributes!的 banged 版本——它会抛出异常,而不是在任何错误时静默返回false。 -
如何生成表单?例如,使用 Rails 表单助手会导致嵌套参数:类似于
params[:uploader][:name](其中:uploader取决于您的模型类。 -
@mudasobwa 谢谢你的提示,我已经改用那个版本了!对于其他人,我添加了日志文件条目和表单本身。提前致谢!
标签: ruby-on-rails ruby