简体   繁体   English

允许用户仅编辑Ruby on Rails上的一些字段

[英]Allow user to edit only some of the fields on Ruby on Rails

How can I achieve that an user could only edit some fields from a record? 如何才能实现用户只能编辑记录中的某些字段?

For example: 例如:

A user can register on the system and select and specify username, password, etc. But a registered user can only edit his name, email and password, not his username. 用户可以在系统上注册并选择并指定用户名,密码等。但是注册用户只能编辑他的姓名,电子邮件和密码,而不能编辑他的用户名。

I have a partial _form, which I use for new and edit functions. 我有一个部分_form,我用它来创建新的和编辑函数。 The partial looks like this: 部分看起来像这样:

    <% form_for @user do |f| %>
    <%= f.error_messages %>
    <p>
            <%= f.label :gruebl_employee_id %><br />
            <%= f.collection_select(:firma_employee_id, FirmaEmployee.find(:all).sort_by{|p| p.lastname.downcase},:id,:full_name, {  :include_blank => "---keine Mitarbeiter---", :prompt => "---Bitte auswählen---"}, { :style => "width: 332px;"}) %>
        </p>
         <%= observe_field(:user_firma_employee_id,
         :frequency => 0.25,
         :url => { :action => :retrieveinfo },
         :with => "'id='+value") %>

        <p>
          <%= f.label :username %><br />
          <%= f.text_field :username %>
        </p>

         <p>
          <%= f.label :admin, "Administrator ?" %><br />
          <%= f.check_box :admin %>
        </p>

        <p>
          <%= f.label :email %><br />
          <%= f.text_field :email %>
        </p>
        <p>
          <%= f.label :password %><br />
          <%= f.password_field :password %>
        </p>
        <p>
          <%= f.label :password_confirmation %><br />
          <%= f.password_field :password_confirmation %>
        </p>
        <p><%= f.submit %></p>
<%end%>

What I am trying to do is block the user from changing the employee and the username attribute. 我想要做的是阻止用户更改员工和用户名属性。

Thanks in advance. 提前致谢。

Take care of this in several places. 在几个地方照顾这个。

First, use attr_accessible in the User model, to include only those you want the user to edit: 首先,在User模型中使用attr_accessible,仅包括您希望用户编辑的那些:

attr_accessible :name, :email, :password, :password_confirmation #...

Then in the form partial, only shows the username field when showing the signup form: 然后在表单partial中,仅在显示注册表单时显示用户名字段:

<% if @user.new_record? %>
  <%= f.text_field :username %>
<% end %>

Lastly, in the create action, manually set the username field, since it's now in the attr_accessible list you cannot mass assign it: 最后,在创建操作中,手动设置用户名字段,因为它现在位于attr_accessible列表中,您无法批量分配它:

@user = User.new(params[:user])
@user.username = params[:user][:username]

Please keep in mind if username is able to be set through mass assignment, even if you disable the username field in the form, it still can be modified (it's easy to change the field value in the form on the client side). 请记住,如果能够通过批量分配设置用户名,即使您在表单中禁用了用户名字段,它仍然可以被修改(很容易在客户端更改表单中的字段值)。 That's why we have to use the attr_accessible for pretection. 这就是我们必须使用attr_accessible进行预检的原因。

Edit: noticed your partial, it's better to protect the admin field as username, otherwise users are able to set themselves as admin by changing the fields value on client side. 编辑:注意到你的部分,最好将admin字段保护为用户名,否则用户可以通过更改客户端的字段值将自己设置为admin。

The trick is to use "object" in conjunction with a label for anything you don't want to change. 诀窍是将“对象”与标签结合使用,以用于您不想更改的任何内容。 Here is how you should code it for the username: 以下是您应该如何为用户名编写代码:

<%= f.label(:username, f.object.username) %>

As I can see you have two ways of doing this. 我可以看到你有两种方法可以做到这一点。

1 - Use the html support for making the textbox readonly. 1 - 使用html支持使文本框只读。 So in your case, user_name textbox will be something like this 所以在你的情况下,user_name文本框将是这样的

<input type="text" name="user_name" value="<User Name>" readonly="readonly" />

2 - But I prefer doing something like creating a read-only textbox in your rails application helper can calling it as and when required 2 - 但我更喜欢在rails应用程序中创建一个只读文本框,helper可以在需要时调用它

So the example would be 所以这个例子就是

in my application helper 在我的应用程序助手中

module ApplicationHelper

    def read_only_text_field(object_name, method, options = {})
          text_field(object_name, method, :disabled => true)
    end
end

and in my view I have 在我看来,我有

<%= read_only_text_field :user, :user_name %>

I think its more DRY/ Railly way. 我认为它更干旱/铁路。

** IMPORTANT - and make sure you doent count the read-only value from your controllers when saving etc.. By that way, even a user changes these readonly values you are still out of the trouble :D **重要 - 并确保在保存等时从控制器中计算只读值。通过这种方式,即使用户更改这些只读值,您仍然无法解决问题:D

hope this helps 希望这可以帮助

cheers 干杯

sameera sameera

Here's my related solution to a similar problem - we have fields that we want a user to be able to set themselves, we don't require them on creation of the record, but we do NOT want to them be changed once they are set. 这是我对类似问题的相关解决方案 - 我们希望用户能够自己设置字段,我们在创建记录时不需要它们,但我们不希望它们在设置后进行更改。

  validate :forbid_changing_some_field, on: :update

  def forbid_changing_some_field
    return unless some_field_changed?
    return if some_field_was.nil?

    self.some_field = some_field_was
    errors.add(:some_field, 'can not be changed!')
  end

The thing that surprised me, though, was that update_attribute still works, it bypasses the validations. 令我感到惊讶的是, update_attribute仍然有效,它绕过了验证。 Not a huge deal, since updates to the record are mass assigned in practice - but I called that out in the tests to make it clear. 这并不是什么大不了的事,因为记录的更新在实践中被大量分配 - 但我在测试中称之为明确。 Here's some tests for it. 这是一些测试。

    describe 'forbids changing some field once set' do
      let(:initial_some_field) { 'initial some field value' }
      it 'defaults as nil' do
        expect(record.some_field).to be nil
      end

      it 'can be set' do
        expect {
          record.update_attribute(:some_field, initial_some_field)
        }.to change {
          record.some_field
        }.from(nil).to(initial_some_field)
      end

      describe 'once it is set' do
        before do
          record.update_attribute(:some_field, initial_some_field)
        end

        it 'makes the record invalid if changed' do
          record.some_field = 'new value'
          expect(record).not_to be_valid
        end

        it 'does not change in mass update' do
          expect {
            record.update_attributes(some_field: 'new value')
          }.not_to change {
            record.some_field
          }.from(initial_some_field)
        end

        it 'DOES change in update_attribute!! (skips validations' do
          expect {
            record.update_attribute(:some_field, 'other new value')
          }.to change {
            record.some_field
          }.from(initial_some_field).to('other new value')
        end
      end
    end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby On Rails Rolify + CanCanCan + Devise允许用户仅编辑其帖子 - Ruby On Rails Rolify + CanCanCan + Devise allow user to edit only their posts 如何只允许Admin(或某些用户)编辑Rails中的页面? - How to allow only Admin (or some user) to edit the pages in Rails? Ruby-Rails如何仅允许当前用户编辑和删除他们创建的帖子 - Ruby-On-Rails How to allow only current user to edit and delete posts they have created 如何只允许帖子的作者编辑或在Ruby on Rails中删除它? - How to allow only the author of a post edit or delete it in Ruby on Rails? 不允许用户在 ruby on rails 中提交带有空字段的表单 - Don't allow user to submit a form with empty fields in ruby on rails 允许用户仅在 ruby​​ on rails 上编辑某些信息,而不能在管理员上编辑 - Allowing users to edit some information only on ruby on rails but not admins 允许用户在rails 4中编辑其个人资料字段 - Allow users to edit their profile fields in rails 4 允许用户将某些项目保存在HTML列表中以供以后编辑(Ruby on Rails) - Allow user to save certain items in HTML list to later edit (Ruby on Rails) 声明式授权:仅允许更新某些字段 - Declarative Authorization: Allow updating some fields only Ruby on Rails:允许用户提交到两个表 - Ruby on Rails: Allow user to submit to two tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM