Reputation: 813
I'm using Rails 4.0.1 and Devise 3.3.2.
I'm trying to create a page where an admin can update multiple users' roles. Something like:
manage.html.erb:
<% @user.each do |user| %>
<%= form_for user, :html => { :multipart => true} do |f| %>
<%= user.profile_name %>
<%= user.right %>
<%= f.label :right %><br />
<%= f.radio_button :right, "None" %>User
<%= f.radio_button :right, "Administrator" %>Administrator</div>
<%= f.submit %>
<% end %>
<% end %>
user_controller.rb:
def manage
@user = User.all
respond_to do |format|
@user.update(user_params)
end
end
private
def user_params
params.require(:user).permit(:id, :right)
end
But I got an error. As I understand, it can't understand the users' id. Any ideas or tutorials?
Started GET "/manage" for 127.0.0.1 at 2013-12-24 22:22:23 +0600
Processing by UserController#update as HTML
Completed 400 Bad Request in 15ms
ActionController::ParameterMissing (param not found: user):
app/controllers/user_controller.rb:46:in `user_params'
app/controllers/user_controller.rb:39:in `block in update'
app/controllers/user_controller.rb:38:in `update'
Upvotes: 0
Views: 214
Reputation: 7014
There are a lot of issues here... but here's where I think your first challenge is:
It looks like you're trying to do both the display of the form and handle the submission of the form in the same action. That's not going to work.
The rails conventions have pairs of actions: new+create and edit+update. new
and edit
only take care of displaying the forms, and create
and update
handle the submission of the forms.
So the first thing you need to do is have two actions in your controller. One will assign @user
then render the view you have, and the other will use the parameters that you get when you submit the form and do something with those parameters to update the users, like what you're trying to do currently in the render
block in your controller.
The other problems you're going to run into next are:
form_for
is not going to post to your custom route unless you make changes to the code you have hereupdate
method on all of them and passing in all the paramsThis railscast on editing multiple records will probably be useful.
Upvotes: 2
Reputation: 15917
When you submit this form, it will use either the create
or update
methods in your controller, and not manage
unless you've specifically added a PATCH
route for it.
So, you'll need to be sure you're using user_params
in those methods.
Additionally, unless it's a typo in your question, you're missing an <% end %>
in manage.html.erb
Upvotes: 1