Rails 用户在自定义路由中更新信息?
Rails Users updating information in custom routes?
不确定如何为我的问题命名...
我的意图是用户将拥有一个项目列表(并且只有 current_user 可以编辑此列表)
class Item < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :items
end
我有一个用户控制器(注意:我也在使用设计 gem,但创建了一个单独的用户控制器)
class UsersController < ApplicationController
load_and_authorize_resource
def show
@user = User.find(params[:id])
end
def list
@user.items.build
end
private
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(
items_attributes: [:user_id, :item, :name, :_destroy]).merge(user_id: current_user.id)
end
end
现在我已经设置路由
get 'users/:id/list', to: 'users#list', as: :list
这给了我这个:localhost:3000/users/1/list
理论上,这应该是显示视图...它将填充用户 ID 的项目列表:1。
我希望能够做到 localhost:3000/list/edit
以便用户可以更新此列表。
我在 views/users/list.html.erb 中的表单(我正在使用 cocoon gem 来帮助处理嵌套表单)。
<%= simple_form_for @user do |f| %>
<%= f.simple_fields_for :items do |i| %>
<%= f.text_field :item %>
<%= f.text_field :name %>
<% end%>
<%= link_to_add_association 'add item', f, :items %>
<%= f.button :submit %>
<% end %>
我知道我的表单在我的列表中。html.erb 它将填充所有项目,但我这样做只是为了测试目的。
如何更新表单以保存到项目数据库及其与当前用户的关联?
编辑:
def update
@user = current_user.items.find_by_user_id(current_user.id)
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'Congrats on your successful update!' }
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
在 "show" 视图中使用表单没有任何问题。如果您只希望 current_user
查看表单,则将您的表单包装在 if 语句中:
<% if @user == current_user %>
(form goes here...)
<% end %>
只要您有 update
route/action users
,就应该提交该表格(免责声明:我从未使用过 cocoon
gem).
编辑:您的表格应该没问题。将此路由添加到您的 routes.rb
文件:
patch 'users/:id', to: 'users#update'
对于当前用户身份验证,您应该使用 cancan
gem
https://github.com/ryanb/cancan
对于与用户相关的嵌套字段 nested_form
gem 是最佳选择
https://github.com/ryanb/nested_form
不确定如何为我的问题命名...
我的意图是用户将拥有一个项目列表(并且只有 current_user 可以编辑此列表)
class Item < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :items
end
我有一个用户控制器(注意:我也在使用设计 gem,但创建了一个单独的用户控制器)
class UsersController < ApplicationController
load_and_authorize_resource
def show
@user = User.find(params[:id])
end
def list
@user.items.build
end
private
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(
items_attributes: [:user_id, :item, :name, :_destroy]).merge(user_id: current_user.id)
end
end
现在我已经设置路由
get 'users/:id/list', to: 'users#list', as: :list
这给了我这个:localhost:3000/users/1/list
理论上,这应该是显示视图...它将填充用户 ID 的项目列表:1。
我希望能够做到 localhost:3000/list/edit
以便用户可以更新此列表。
我在 views/users/list.html.erb 中的表单(我正在使用 cocoon gem 来帮助处理嵌套表单)。
<%= simple_form_for @user do |f| %>
<%= f.simple_fields_for :items do |i| %>
<%= f.text_field :item %>
<%= f.text_field :name %>
<% end%>
<%= link_to_add_association 'add item', f, :items %>
<%= f.button :submit %>
<% end %>
我知道我的表单在我的列表中。html.erb 它将填充所有项目,但我这样做只是为了测试目的。
如何更新表单以保存到项目数据库及其与当前用户的关联?
编辑:
def update
@user = current_user.items.find_by_user_id(current_user.id)
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'Congrats on your successful update!' }
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
在 "show" 视图中使用表单没有任何问题。如果您只希望 current_user
查看表单,则将您的表单包装在 if 语句中:
<% if @user == current_user %>
(form goes here...)
<% end %>
只要您有 update
route/action users
,就应该提交该表格(免责声明:我从未使用过 cocoon
gem).
编辑:您的表格应该没问题。将此路由添加到您的 routes.rb
文件:
patch 'users/:id', to: 'users#update'
对于当前用户身份验证,您应该使用 cancan
gem
https://github.com/ryanb/cancan
对于与用户相关的嵌套字段 nested_form
gem 是最佳选择
https://github.com/ryanb/nested_form