Ruby on Rails:params [] Controller中的变量

时间:2015-12-09 12:32:37

标签: ruby-on-rails ruby

我是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”的表单元素中的值吗?或者我误解了它的目的?

修改

回答一些问题:

我相信这是相关的日志条目,对我来说一切都好吗?

Started PATCH "/s3_destinations/1" for 127.0.0.1 at 2015-12-09 12:40:51 +0000
Processing by S3DestinationsController#update as HTML
  Parameters: {"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"=>"Save S3 destination", "id"=>"1"}
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  S3Destination Load (0.0ms)  SELECT  "s3_destinations".* FROM "s3_destinations" WHERE "s3_destinations"."id" = ? LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
   (0.0ms)  commit transaction
Redirected to 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 %>

1 个答案:

答案 0 :(得分:1)

您引用了params[:name],但参数都在params[:s3_destination]下捆绑在一起,因此您需要引用params[:s3_destination][:name]等。

关于这一点的好处是你不需要调出你想要更新的每个属性。在简单的情况下,你可以

@s3_destination.update_attributes(params[:s3_destination])

为了安全起见(例如,我无法编辑表单以提交您不希望我编辑的属性),您应该使用strong parameters

s3_params = params.require(:s3_destination)
  .permit(:name, :bucket, :region, :credentials_id, :credentials_secret)
@s3_destination.update_attributes(s3_params)

然后对于credentials_secret逻辑,您只需从此哈希中删除空白值:

s3_params = params.require(:s3_destination)
  .permit(:name, :bucket, :region, :credentials_id, :credentials_secret)
s3_params.delete(:credentials_secret) if s3_params[:credentials_secret].blank?
@s3_destination.update_attributes(s3_params)

blank?将以nil的方式同时捕捉"".to_s.empty?