only edit certain fields in Rails
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
add a comment |
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
add a comment |
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
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
ruby-on-rails ruby-on-rails-5
edited Nov 22 '18 at 17:14
random_user_0891
asked Nov 22 '18 at 17:05
random_user_0891random_user_0891
5511417
5511417
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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"
add a comment |
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.
add a comment |
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
When he wants to edit a single object then why would he require a new model?
– Manishh
Nov 22 '18 at 17:43
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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"
add a comment |
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"
add a comment |
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"
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"
answered Nov 22 '18 at 19:31
engineerDaveengineerDave
3,6051925
3,6051925
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 22 '18 at 19:36
cd-rumcd-rum
3,50632860
3,50632860
add a comment |
add a comment |
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
When he wants to edit a single object then why would he require a new model?
– Manishh
Nov 22 '18 at 17:43
add a comment |
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
When he wants to edit a single object then why would he require a new model?
– Manishh
Nov 22 '18 at 17:43
add a comment |
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
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
answered Nov 22 '18 at 17:42
Clyde TClyde T
769
769
When he wants to edit a single object then why would he require a new model?
– Manishh
Nov 22 '18 at 17:43
add a comment |
When he wants to edit a single object then why would he require a new model?
– Manishh
Nov 22 '18 at 17:43
When he wants to edit a single object then why would he require a new model?
– Manishh
Nov 22 '18 at 17:43
When he wants to edit a single object then why would he require a new model?
– Manishh
Nov 22 '18 at 17:43
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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