0

私は最適なgemを使用していますが、ログインしていない(現在のユーザーではない)ユーザープロファイルのフィールドを読み取り専用モードにしたいと考えています。ユーザーは ATM にログインし、別のユーザーのページにアクセスしてプロファイル オプションを変更できます (データベースには保存されません)。あなたが現在のユーザーでない場合、すべてのプロファイル情報が読み取り専用になるように設定する必要があります。

show.html.erb:

<p>Education: <%= best_in_place @user, :education, nil: 'What is your education level?', :type => :select, :collection => [["High school", "High school"], ["Some college", "Some college"], ["Undergraduate", "Undergraduate"], ["Bachelor's", "Bachelor's"], ["Master's", "Master's"], ["PhD", "PhD"], ["Business school", "Business school"], ["Law school", "Law school"], ["Medical school", "Medical school"]] %></p>

ユーザーコントローラー:

   def update
      @user = if current_user.has_role?(:admin)
            User.find(params[:id])
          else
            current_user
          end
         @user.update_attributes(params[:user])
         respond_with @user
        end

 def edit
      @user = User.find(params[:id])
end
4

1 に答える 1

1

最高のインプレースgemが何をするのかはわかりませんが、通常のフォームの場合readonly: true、ビューのフィールドにオプションを追加する必要があります. あなたのビューはインスタンス変数editもビューに提供すると仮定しました。@user

何かのようなもの:

<% if current_user.id == @user.id %>
  <p>Education: <%= best_in_place @user, :education, nil: 'What is your education level?', :type => :select, :collection => [["High school", "High school"], ["Some college", "Some college"], ["Undergraduate", "Undergraduate"], ["Bachelor's", "Bachelor's"], ["Master's", "Master's"], ["PhD", "PhD"], ["Business school", "Business school"], ["Law school", "Law school"], ["Medical school", "Medical school"]] %></p>
<% else %>
  <p>Education: <%= best_in_place @user, :education, nil: 'What is your education level?', :type => :select, :collection => [["High school", "High school"], ["Some college", "Some college"], ["Undergraduate", "Undergraduate"], ["Bachelor's", "Bachelor's"], ["Master's", "Master's"], ["PhD", "PhD"], ["Business school", "Business school"], ["Law school", "Law school"], ["Medical school", "Medical school"]], readonly: true %></p>
<% end %>

それが役立つことを願っています。

編集:

通常の Rails ヘルパー オプションは利用できないようです。そのため、次のように HTML の disabled 属性を直接追加してみてください。

<% if current_user.id == @user.id %>
  <p>Education: <%= best_in_place @user, :education, nil: 'What is your education level?', :type => :select, :collection => [["High school", "High school"], ["Some college", "Some college"], ["Undergraduate", "Undergraduate"], ["Bachelor's", "Bachelor's"], ["Master's", "Master's"], ["PhD", "PhD"], ["Business school", "Business school"], ["Law school", "Law school"], ["Medical school", "Medical school"]] %></p>
<% else %>
  <p>Education: <%= best_in_place @user, :education, nil: 'What is your education level?', :type => :select, :collection => [["High school", "High school"], ["Some college", "Some college"], ["Undergraduate", "Undergraduate"], ["Bachelor's", "Bachelor's"], ["Master's", "Master's"], ["PhD", "PhD"], ["Business school", "Business school"], ["Law school", "Law school"], ["Medical school", "Medical school"]], :html_attrs => {:disabled => true} %></p>
<% end %>
于 2013-12-02T21:39:13.100 に答える