I have an Admin Controller for a page of a Rail 3 app that lists all user model accounts in the table , And the part should be a link (with Confirmation) of each line to toggle the is_admin attribute of my user model. I would like to do this by allowing an administrator to click in the table cell, which will be the current admin status To list Does (ie "will not be set right is_admin clicking on").
Right now, there is no interest in any fancy AJAX approach, and ignores the fact that from the UI perspective, clicking on the text that points to the position of an object to point to that position This is not the best option.
Am I thinking, can it be done for the same link, or do I need to build a form around it? My admin controller is defined as 'resources': path.rb and rake routes
have a list of PUT actions with the named path of 'admin' which invokes the admin # update method Does.
Even what I have done:
& lt;% = link_to (user.is_admin? "Yes": "no"), admin_path ( : User = & gt; {: id = & gt; user.id,: is_admin = & gt; USER.is_admin}), {: confirmation => "Are you sure?" ,: Method = & gt; : Put}%>
I think that I am passing enough data in a quiet call in the update method so that I can modify that attribute. I know that it would be possible to define a method called just toggle_admin
, just have to go through the ID and work on that member of that model, but I thought this approach would be as meaningful as possible, I I know that I can use link_to here
My mistake is what I'm getting:
matches any routes {: action = & gt; "Destroyed" ,: controller => "Admin" ,: User = & gt; {: Id = & gt; 1,: is_admin = & gt; Wrong}}
I'm not sure why this is trying to implement the destruct method, this is the last thing in my admin special path list:
< Pre> rake routes grep admin site_admin /admin(.:format) {: controller => "Administrator" ,: verb = & gt; "Index"} admin_index GET / admin (.format) {: action => "Index",: Controller = & Gt; "Admin"} admin_index POST / Admin (.: Format) {: Action => "Create",: admin = & gt; "Admin"} new_admin GET / admin / new (.: Format) {: action = & gt; "New" ,: admin = & gt; "Admin"} edit_admin GET /admin/:id/edit(.:format) {: action => "Edit",: admin = & gt; "Admin"} Admin GET /admin/:id(.format) {: action = & gt; "Show",: Controller => "Admin"} Admin PUT /admin/:id(.:format) {: action = & gt; "Update" administrator: admin controller = & gt; "Admin"} Admin DELETE /admin/:id(.:format) {: action = & gt; "Destroyed" ,: controller => "Admin"}
I thought I am passing through the wrong way and it is a mistake for the last one, but the tip is the cap which you can call : method = & gt; ; As an HTML option, you can set
with your : confirm
example.
So fundamentally, my question is, should I be able to make a conditional non-GET call through link_to this way? I assumed that the rail would be a small form behind the curtain for me.
More source code.
Use this:
div_for @user do link_to (@ User.is_admin? "Yes": "no"), [user, toggle],: confirmation => ... end
and this route:
map.resources: user ,: as in => "Admin" ,: Member = & gt; {: Toggle = & gt; : Put}
and this control method:
class UsersController & lt; ... DF Toggle @ User = User.Fond Paramos [: ID],: Lock => True @ user.is_admin = !@user.is_admin? @ User.save! If request.xhr? Render: updates {| Page | Page [user] .replace_html @ user] Other redirect_to @user end end end
Attempting to handle this link with the update action becomes very complicated for just one case (as That you already know that there are some disadvantages). Using the link_to_remote
for this case is a good candidate (just because I'm already showing AJAX handling that it is not very fancy).
Comments
Post a Comment