rails 和 ruby 的新手。与 form_for
和关联作斗争。我正在尝试设置一个允许用户从客户端列表中进行选择的 Rails 应用程序。客户端通过 has_many :through
关系关联。我的模型按预期工作,我可以通过 Rails 控制台向用户添加客户端。我现在想将此功能移至 Web 界面。下面的代码是我尝试过的,但对我来说没有意义。我不确定哪个 Controller 操作是正确的操作。我应该在客户端的创建操作中处理表单 POST
吗?我实际上并不想创建新客户,我只是希望用户从现有客户列表中进行选择并创建关联。
我的模型如下:
class Client < ActiveRecord::Base
has_many :users, :through => :user_clients
has_many :user_clients, :dependent => :destroy
end
class User < ActiveRecord::Base
has_many :clients, :through => :user_clients
has_many :user_clients, :dependent => :destroy
end
class UserClient < ActiveRecord::Base
belongs_to :user
belongs_to :client
end
路线如下
resources :clients do
resources :users
end
resources :users do
resources :clients
end
客户端 Controller
class ClientsController < ApplicationController
before_action :set_client, only: [:show, :edit, :update, :destroy]
def index
if params[:user_id]
@clients = User.find_by_id(params[:user_id]).clients
else
@clients = Client.all
end
@clients
end
def create
if params[:user_id]
user = User.find(params[:user_id])
client = Client.find_by_id(params[:client_id])
user.clients << client
user.save
redirect_to users_url
else
@client = Client.new(client_params)
@client.save
redirect_to clients_url
end
end
end
客户端 View 中的表单
<h1>new.html.erb</h1>
<% if params[:user_id] %>
<%= form_for([@user,@client]) do |f| %>
<div class="field">
<%= f.label :id %><br>
<%= f.text_field :id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% else %>
<%= render 'form' %>
<% end %>
最佳答案
首先,看看那个 gem:Nested Form .这里还有几个 railscasts关于这个话题。但是,如果您愿意构建自己的解决方案,可以先阅读有关嵌套属性的文档 here .
希望对您有所帮助!
更新。关于你的路线和一切。
resources :clients do
resources :users
end
resources :users do
resources :clients
end
您在此处嵌套了资源。正如您在评论中提到的,users/:user_id/clients/new
和 clients/new
将导致相同的操作。这就是路由的工作方式。您对 Rails 做事风格的看法是正确的 - 在 Controller 中进行一些可怕的逻辑检查并基本上在一个 Action 中做不同的事情是非常糟糕的。由于您的操作(将用户与现有客户端相关联)与一组标准的 RESTful 操作相去甚远(尽管最接近的变体将是 update
),您需要为您的资源引入新的操作 - 例如 assign_client
。你可以这样做:
resources :users do
resources :clients
member do
put :assign_client
end
end
您可以在任何您喜欢的 Action 中渲染表单。但是,如果您想要更独立的东西,您可以创建一对操作(类似于edit
和update
对)。第一个将呈现适当的表单,第二个将在服务器上处理该表单。
关于ruby-on-rails - rails 4 : form_for with associations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21729684/