简体   繁体   中英

Rails: saving a user field from another controller

Combo: Rails4, Mongoid I have an Admin console dashboard: /admin/moderator-slug/dashboard that lists all users with a button to go manage that user:

<%= link_to "Manage", { controller: 'admin', action: 'manage', user: user} %>

As you can see, I pass that user in the params. In my Admin controller, my manage action is as follows:

def manage
  @user = User.find(params[:user])
  if params[:follow_up].present?
    @user.follow_up = Date.strptime(params[:follow_up], "%m/%d/%Y")
  end
end

It successfully leads to the management page: /admin/moderator-slug/manage?user=managed-user-slug and finds the @user through the params I pass with the button.

In my User model I created a Time field "follow_up".

I want to update that field with a certain time selected with datepicker, so I created the following form_tag:

<%= form_tag manage_path, method: "get", remote: true do %>
  Follow up: <%= text_field_tag "follow_up", nil, autocomplete: "off" %>
  <%= submit_tag "Create" %>
<% end %>

When I press the submit button I get Mongoid::Errors::InvalidFind

When I pass on a hidden field <%= hidden_field_tag :user , @user %> , I get 404. And besides, I have a feeling my set up is not the healthiest, so passing on that hidden field is really a workaround that could be avoided.

my routes:

authenticate :user, -> (u) { u.is_moderator? } do
  match 'admin/:slug/dashboard',    to: 'admin/admin#dashboard',    via: 'get',   as: :dashboard
  match 'admin/:slug/manage',       to: 'admin/admin#manage',       via: 'get',   as: :manage
end

Any suggestions?

看起来您应该将用户条目添加到表单URL:

<%= form_tag manage_path(user: @user), method: "get", remote: true do %>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM