i have the following situation: I have a controller for admins , that i use to manage the users (model generated by Devise).
So, i use the actions of another controller to manage the resources "users".
Here my files:
admins_controller.rb
before_action :set_user, only: [:show, :edit, :update, :destroy]
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to admins_index_path, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:id)
end
edit.html.erb
<h1>Edit user</h1>
<%= form_for @user, :as => :patch, :url => admins_update_path(id: @user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this line_item from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :id %><br>
<%= f.text_field :id %>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :admin %><br>
<%= f.text_field :admin %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The problem is that when i submit the edit , rails gives me an error:
Can you help me? i really don t knok how to fix thank you
EDIT:
Change your view to drop the :as
option, that will fix the params requirement error:
= form_for @user, :url => admins_update_path(id: @user) do |f|
The :as
tells form_for
to use a different key name than the default class-name based one that you're using in your params.require
call.
You should also remove your f.text_field :id
- you don't want someone editing that.
Then your permit(...)
block will need to include anything else in that form that you want to allow mass-assignment for, most likely you'll want: permit(:email, :admin)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.