only edit certain fields in Rails











up vote
1
down vote

favorite












I have a user model that has fields for first_name, last_name, email, and password. After a user signs up under their user profile page I would like to allow certain fields and not others to show on a form to update part of the user info.



For example I would like just the first_name to be shown as a field when I render a form for them to edit their name. How do I handle this? Should I make a new route and a controller action that lets me update just this one field? or do I just hide all the other fields and submit them in the background without the user seeing them?



<div class="signup_user">
<div class="container-fluid">
<div class="col-md-5 col-sm-12 centered">

<%= form_for(@user) do |form| %>

<%= render "shared/errors", object: @user %>
<%= render "shared/flash" %>

<div class="card card-default">
<div class="card-body">

<h1>Create account</h1>

<%= form.label :first_name %>
<%= form.text_field :first_name, size: 40, autofocus: true, class: "required form-control" %>

<%= form.label :last_name %>
<%= form.text_field :last_name, size: 40, class: "required form-control" %>

<%= form.label :email %>
<%= form.email_field :email, size: 40, class: "required form-control" %>

<%= form.label :password %>
<%= form.password_field :password, size: 40, placeholder: "At least 10 characters", class: "required form-control" %>

<%= form.label :password_confirmation, "Confirm Password" %>
<%= form.password_field :password_confirmation, size: 40, class: "required form-control" %><br>

<%= form.submit "Submit", class: 'btn btn-block btn-outline-primary' %>

<% end %>

<hr>
<p>Already have an account? <%= link_to "Sign In", new_session_path %></p>

</div>
</div>

</div>
</div><!--./container-->
</div><!--./signup_user-->


after the initial signup I would like to just show 1 field like this for editing just the user name.



<div class="signup_user">
<div class="container-fluid">
<div class="col-md-5 col-sm-12 centered">

<%= form_for(@user) do |form| %>

<%= render "shared/errors", object: @user %>
<%= render "shared/flash" %>

<div class="card card-default">
<div class="card-body">


<%= form.label :first_name %>
<%= form.text_field :first_name, size: 40, autofocus: true, class: "required form-control" %>



</div>
</div><!--./container-->
</div><!--./signup_user-->









share|improve this question




























    up vote
    1
    down vote

    favorite












    I have a user model that has fields for first_name, last_name, email, and password. After a user signs up under their user profile page I would like to allow certain fields and not others to show on a form to update part of the user info.



    For example I would like just the first_name to be shown as a field when I render a form for them to edit their name. How do I handle this? Should I make a new route and a controller action that lets me update just this one field? or do I just hide all the other fields and submit them in the background without the user seeing them?



    <div class="signup_user">
    <div class="container-fluid">
    <div class="col-md-5 col-sm-12 centered">

    <%= form_for(@user) do |form| %>

    <%= render "shared/errors", object: @user %>
    <%= render "shared/flash" %>

    <div class="card card-default">
    <div class="card-body">

    <h1>Create account</h1>

    <%= form.label :first_name %>
    <%= form.text_field :first_name, size: 40, autofocus: true, class: "required form-control" %>

    <%= form.label :last_name %>
    <%= form.text_field :last_name, size: 40, class: "required form-control" %>

    <%= form.label :email %>
    <%= form.email_field :email, size: 40, class: "required form-control" %>

    <%= form.label :password %>
    <%= form.password_field :password, size: 40, placeholder: "At least 10 characters", class: "required form-control" %>

    <%= form.label :password_confirmation, "Confirm Password" %>
    <%= form.password_field :password_confirmation, size: 40, class: "required form-control" %><br>

    <%= form.submit "Submit", class: 'btn btn-block btn-outline-primary' %>

    <% end %>

    <hr>
    <p>Already have an account? <%= link_to "Sign In", new_session_path %></p>

    </div>
    </div>

    </div>
    </div><!--./container-->
    </div><!--./signup_user-->


    after the initial signup I would like to just show 1 field like this for editing just the user name.



    <div class="signup_user">
    <div class="container-fluid">
    <div class="col-md-5 col-sm-12 centered">

    <%= form_for(@user) do |form| %>

    <%= render "shared/errors", object: @user %>
    <%= render "shared/flash" %>

    <div class="card card-default">
    <div class="card-body">


    <%= form.label :first_name %>
    <%= form.text_field :first_name, size: 40, autofocus: true, class: "required form-control" %>



    </div>
    </div><!--./container-->
    </div><!--./signup_user-->









    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a user model that has fields for first_name, last_name, email, and password. After a user signs up under their user profile page I would like to allow certain fields and not others to show on a form to update part of the user info.



      For example I would like just the first_name to be shown as a field when I render a form for them to edit their name. How do I handle this? Should I make a new route and a controller action that lets me update just this one field? or do I just hide all the other fields and submit them in the background without the user seeing them?



      <div class="signup_user">
      <div class="container-fluid">
      <div class="col-md-5 col-sm-12 centered">

      <%= form_for(@user) do |form| %>

      <%= render "shared/errors", object: @user %>
      <%= render "shared/flash" %>

      <div class="card card-default">
      <div class="card-body">

      <h1>Create account</h1>

      <%= form.label :first_name %>
      <%= form.text_field :first_name, size: 40, autofocus: true, class: "required form-control" %>

      <%= form.label :last_name %>
      <%= form.text_field :last_name, size: 40, class: "required form-control" %>

      <%= form.label :email %>
      <%= form.email_field :email, size: 40, class: "required form-control" %>

      <%= form.label :password %>
      <%= form.password_field :password, size: 40, placeholder: "At least 10 characters", class: "required form-control" %>

      <%= form.label :password_confirmation, "Confirm Password" %>
      <%= form.password_field :password_confirmation, size: 40, class: "required form-control" %><br>

      <%= form.submit "Submit", class: 'btn btn-block btn-outline-primary' %>

      <% end %>

      <hr>
      <p>Already have an account? <%= link_to "Sign In", new_session_path %></p>

      </div>
      </div>

      </div>
      </div><!--./container-->
      </div><!--./signup_user-->


      after the initial signup I would like to just show 1 field like this for editing just the user name.



      <div class="signup_user">
      <div class="container-fluid">
      <div class="col-md-5 col-sm-12 centered">

      <%= form_for(@user) do |form| %>

      <%= render "shared/errors", object: @user %>
      <%= render "shared/flash" %>

      <div class="card card-default">
      <div class="card-body">


      <%= form.label :first_name %>
      <%= form.text_field :first_name, size: 40, autofocus: true, class: "required form-control" %>



      </div>
      </div><!--./container-->
      </div><!--./signup_user-->









      share|improve this question















      I have a user model that has fields for first_name, last_name, email, and password. After a user signs up under their user profile page I would like to allow certain fields and not others to show on a form to update part of the user info.



      For example I would like just the first_name to be shown as a field when I render a form for them to edit their name. How do I handle this? Should I make a new route and a controller action that lets me update just this one field? or do I just hide all the other fields and submit them in the background without the user seeing them?



      <div class="signup_user">
      <div class="container-fluid">
      <div class="col-md-5 col-sm-12 centered">

      <%= form_for(@user) do |form| %>

      <%= render "shared/errors", object: @user %>
      <%= render "shared/flash" %>

      <div class="card card-default">
      <div class="card-body">

      <h1>Create account</h1>

      <%= form.label :first_name %>
      <%= form.text_field :first_name, size: 40, autofocus: true, class: "required form-control" %>

      <%= form.label :last_name %>
      <%= form.text_field :last_name, size: 40, class: "required form-control" %>

      <%= form.label :email %>
      <%= form.email_field :email, size: 40, class: "required form-control" %>

      <%= form.label :password %>
      <%= form.password_field :password, size: 40, placeholder: "At least 10 characters", class: "required form-control" %>

      <%= form.label :password_confirmation, "Confirm Password" %>
      <%= form.password_field :password_confirmation, size: 40, class: "required form-control" %><br>

      <%= form.submit "Submit", class: 'btn btn-block btn-outline-primary' %>

      <% end %>

      <hr>
      <p>Already have an account? <%= link_to "Sign In", new_session_path %></p>

      </div>
      </div>

      </div>
      </div><!--./container-->
      </div><!--./signup_user-->


      after the initial signup I would like to just show 1 field like this for editing just the user name.



      <div class="signup_user">
      <div class="container-fluid">
      <div class="col-md-5 col-sm-12 centered">

      <%= form_for(@user) do |form| %>

      <%= render "shared/errors", object: @user %>
      <%= render "shared/flash" %>

      <div class="card card-default">
      <div class="card-body">


      <%= form.label :first_name %>
      <%= form.text_field :first_name, size: 40, autofocus: true, class: "required form-control" %>



      </div>
      </div><!--./container-->
      </div><!--./signup_user-->






      ruby-on-rails ruby-on-rails-5






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 17:14

























      asked Nov 22 at 17:05









      SMK

      507316




      507316
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          The easiest way is use the action_name helper and create some conditional logic that will conditionally render the fields that you only want to show on the new action, e.g. action_name == "new"






          share|improve this answer




























            up vote
            1
            down vote













            I'd make two definitions of strong parameters on the controller:



            def create_params
            params.require(:user).permit(:first_name, :last_name, :email, :password)
            end

            def update_params
            params.require(:user).permit(:email, :password)
            end


            Then in your create/update methods just allow each different definition. You don't need a new view or a new model if you just restrict the params based on the action.



            def create
            @user.create(create_params)
            redirect_to users_path
            end

            def update
            @user.update_attributes(update_params)
            redirect_to users_path
            end


            Makes sense to me, and that way you don't have to create anything superfluous. Then fields will be rejected in a secure way the way they should be. Then you can adjust the view with action_name == 'edit' or similar.






            share|improve this answer




























              up vote
              -1
              down vote













              You can generate an additional view by creating a new model.



              Example :



              models/user_edit.rb



              class User_Edit < User
              end


              models/user.rb



              class User < ApplicationRecord
              ...
              end


              routes



                resources :users
              devise_for :users, path: 'users', controllers: {
              sessions: "users/sessions",
              passwords: "users/passwords",
              registrations: "users/registrations",
              confirmations: "users/confirmations",
              }
              devise_for :users_edit, path: 'users_edit', controllers: {
              registrations: "users_edit/registrations",
              }


              And so you will have :



              views/users/registrations/edit.html


              and



              views/users_edit/registrations/edit.html





              share|improve this answer





















              • When he wants to edit a single object then why would he require a new model?
                – Manishh
                Nov 22 at 17:43











              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435578%2fonly-edit-certain-fields-in-rails%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote













              The easiest way is use the action_name helper and create some conditional logic that will conditionally render the fields that you only want to show on the new action, e.g. action_name == "new"






              share|improve this answer

























                up vote
                1
                down vote













                The easiest way is use the action_name helper and create some conditional logic that will conditionally render the fields that you only want to show on the new action, e.g. action_name == "new"






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  The easiest way is use the action_name helper and create some conditional logic that will conditionally render the fields that you only want to show on the new action, e.g. action_name == "new"






                  share|improve this answer












                  The easiest way is use the action_name helper and create some conditional logic that will conditionally render the fields that you only want to show on the new action, e.g. action_name == "new"







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 at 19:31









                  engineerDave

                  3,6101925




                  3,6101925
























                      up vote
                      1
                      down vote













                      I'd make two definitions of strong parameters on the controller:



                      def create_params
                      params.require(:user).permit(:first_name, :last_name, :email, :password)
                      end

                      def update_params
                      params.require(:user).permit(:email, :password)
                      end


                      Then in your create/update methods just allow each different definition. You don't need a new view or a new model if you just restrict the params based on the action.



                      def create
                      @user.create(create_params)
                      redirect_to users_path
                      end

                      def update
                      @user.update_attributes(update_params)
                      redirect_to users_path
                      end


                      Makes sense to me, and that way you don't have to create anything superfluous. Then fields will be rejected in a secure way the way they should be. Then you can adjust the view with action_name == 'edit' or similar.






                      share|improve this answer

























                        up vote
                        1
                        down vote













                        I'd make two definitions of strong parameters on the controller:



                        def create_params
                        params.require(:user).permit(:first_name, :last_name, :email, :password)
                        end

                        def update_params
                        params.require(:user).permit(:email, :password)
                        end


                        Then in your create/update methods just allow each different definition. You don't need a new view or a new model if you just restrict the params based on the action.



                        def create
                        @user.create(create_params)
                        redirect_to users_path
                        end

                        def update
                        @user.update_attributes(update_params)
                        redirect_to users_path
                        end


                        Makes sense to me, and that way you don't have to create anything superfluous. Then fields will be rejected in a secure way the way they should be. Then you can adjust the view with action_name == 'edit' or similar.






                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          I'd make two definitions of strong parameters on the controller:



                          def create_params
                          params.require(:user).permit(:first_name, :last_name, :email, :password)
                          end

                          def update_params
                          params.require(:user).permit(:email, :password)
                          end


                          Then in your create/update methods just allow each different definition. You don't need a new view or a new model if you just restrict the params based on the action.



                          def create
                          @user.create(create_params)
                          redirect_to users_path
                          end

                          def update
                          @user.update_attributes(update_params)
                          redirect_to users_path
                          end


                          Makes sense to me, and that way you don't have to create anything superfluous. Then fields will be rejected in a secure way the way they should be. Then you can adjust the view with action_name == 'edit' or similar.






                          share|improve this answer












                          I'd make two definitions of strong parameters on the controller:



                          def create_params
                          params.require(:user).permit(:first_name, :last_name, :email, :password)
                          end

                          def update_params
                          params.require(:user).permit(:email, :password)
                          end


                          Then in your create/update methods just allow each different definition. You don't need a new view or a new model if you just restrict the params based on the action.



                          def create
                          @user.create(create_params)
                          redirect_to users_path
                          end

                          def update
                          @user.update_attributes(update_params)
                          redirect_to users_path
                          end


                          Makes sense to me, and that way you don't have to create anything superfluous. Then fields will be rejected in a secure way the way they should be. Then you can adjust the view with action_name == 'edit' or similar.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 22 at 19:36









                          well-i-better-get-rolling

                          3,37932759




                          3,37932759






















                              up vote
                              -1
                              down vote













                              You can generate an additional view by creating a new model.



                              Example :



                              models/user_edit.rb



                              class User_Edit < User
                              end


                              models/user.rb



                              class User < ApplicationRecord
                              ...
                              end


                              routes



                                resources :users
                              devise_for :users, path: 'users', controllers: {
                              sessions: "users/sessions",
                              passwords: "users/passwords",
                              registrations: "users/registrations",
                              confirmations: "users/confirmations",
                              }
                              devise_for :users_edit, path: 'users_edit', controllers: {
                              registrations: "users_edit/registrations",
                              }


                              And so you will have :



                              views/users/registrations/edit.html


                              and



                              views/users_edit/registrations/edit.html





                              share|improve this answer





















                              • When he wants to edit a single object then why would he require a new model?
                                – Manishh
                                Nov 22 at 17:43















                              up vote
                              -1
                              down vote













                              You can generate an additional view by creating a new model.



                              Example :



                              models/user_edit.rb



                              class User_Edit < User
                              end


                              models/user.rb



                              class User < ApplicationRecord
                              ...
                              end


                              routes



                                resources :users
                              devise_for :users, path: 'users', controllers: {
                              sessions: "users/sessions",
                              passwords: "users/passwords",
                              registrations: "users/registrations",
                              confirmations: "users/confirmations",
                              }
                              devise_for :users_edit, path: 'users_edit', controllers: {
                              registrations: "users_edit/registrations",
                              }


                              And so you will have :



                              views/users/registrations/edit.html


                              and



                              views/users_edit/registrations/edit.html





                              share|improve this answer





















                              • When he wants to edit a single object then why would he require a new model?
                                – Manishh
                                Nov 22 at 17:43













                              up vote
                              -1
                              down vote










                              up vote
                              -1
                              down vote









                              You can generate an additional view by creating a new model.



                              Example :



                              models/user_edit.rb



                              class User_Edit < User
                              end


                              models/user.rb



                              class User < ApplicationRecord
                              ...
                              end


                              routes



                                resources :users
                              devise_for :users, path: 'users', controllers: {
                              sessions: "users/sessions",
                              passwords: "users/passwords",
                              registrations: "users/registrations",
                              confirmations: "users/confirmations",
                              }
                              devise_for :users_edit, path: 'users_edit', controllers: {
                              registrations: "users_edit/registrations",
                              }


                              And so you will have :



                              views/users/registrations/edit.html


                              and



                              views/users_edit/registrations/edit.html





                              share|improve this answer












                              You can generate an additional view by creating a new model.



                              Example :



                              models/user_edit.rb



                              class User_Edit < User
                              end


                              models/user.rb



                              class User < ApplicationRecord
                              ...
                              end


                              routes



                                resources :users
                              devise_for :users, path: 'users', controllers: {
                              sessions: "users/sessions",
                              passwords: "users/passwords",
                              registrations: "users/registrations",
                              confirmations: "users/confirmations",
                              }
                              devise_for :users_edit, path: 'users_edit', controllers: {
                              registrations: "users_edit/registrations",
                              }


                              And so you will have :



                              views/users/registrations/edit.html


                              and



                              views/users_edit/registrations/edit.html






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 22 at 17:42









                              Clyde T

                              769




                              769












                              • When he wants to edit a single object then why would he require a new model?
                                – Manishh
                                Nov 22 at 17:43


















                              • When he wants to edit a single object then why would he require a new model?
                                – Manishh
                                Nov 22 at 17:43
















                              When he wants to edit a single object then why would he require a new model?
                              – Manishh
                              Nov 22 at 17:43




                              When he wants to edit a single object then why would he require a new model?
                              – Manishh
                              Nov 22 at 17:43


















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435578%2fonly-edit-certain-fields-in-rails%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Trompette piccolo

                              Slow SSRS Report in dynamic grouping and multiple parameters

                              Simon Yates (cyclisme)