[POST] [Ruby on Rails] 上的路由错误
现在我正在 Rails 中构建一个项目管理应用程序,这里有一些背景信息:
现在我有 2 个模型,一个是用户,另一个是客户端。客户端和用户具有一对一的关系(客户端 -> has_one 和用户 -> Members_to 这意味着外键位于用户表中)
所以我想做的是一旦你添加了一个客户端实际上可以向该客户端添加凭据(添加用户),为此,所有客户端都将显示在该客户端名称旁边的链接,这意味着您实际上可以为该客户端创建凭据。
因此,为了做到这一点,我使用了一个助手,就像这样的助手链接。
<%= link_to "Credentials",
{:controller => 'user', :action => 'new', :client_id => client.id} %>
这意味着他的 url 将像这样构造:
http://localhost:3000/clients/2/user/new
通过为 ID 为 2 的客户端创建用户。
然后将信息捕获到控制器中,如下所示:
@user = User.new(:client_id => params[:client_id])
编辑:这是我当前在视图/控制器中拥有的内容和路由
我不断收到此错误:没有路由与 {:method=>:post} 匹配“/clients//user”
路由
ActionController::Routing::Routes.draw do |map|
map.resources :users
map.resources :clients, :has_one => :user
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
控制器
class UsersController < ApplicationController
before_filter :load_client
def new
@user = User.new
@client = Client.new
end
def load_client
@client = Client.find(params[:client_id])
end
def create
@user = User.new(params[:user])
@user.client_id = @client.id
if @user.save
flash[:notice] = "Credentials created"
render :new
else
flash[:error] = "Credentials created failed"
render :new
end
end
视图
<% form_for @user, :url => client_user_url(@client) do |f| %>
<p>
<%= f.label :login, "Username" %>
<%= f.text_field :login %>
</p>
<p>
<%= f.label :password, "Password" %>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation, "Password Confirmation" %>
<%= f.password_field :password_confirmation %>
</p>
<%= f.submit "Create", :disable_with => 'Please Wait...' %>
<% end %>
Right now I'm building a project management app in rails, here is some background info:
Right now i have 2 models, one is User and the other one is Client. Clients and Users have a one-to-one relationship (client -> has_one and user -> belongs_to which means that the foreign key it's in the users table)
So what I'm trying to do it's once you add a client you can actually add credentials (add an user) to that client, in order to do so all the clients are being displayed with a link next to that client's name meaning that you can actually create credentials for that client.
So in order to do that I'm using a helper the link to helper like this.
<%= link_to "Credentials",
{:controller => 'user', :action => 'new', :client_id => client.id} %>
Meaning that he url will be constructed like this:
http://localhost:3000/clients/2/user/new
By creating the user for the client with he ID of 2.
And then capturing the info into the controller like this:
@user = User.new(:client_id => params[:client_id])
EDIT: This is what i currently have in my View/Controller and Routes
I keep getting this error: No route matches "/clients//user" with {:method=>:post}
Routes
ActionController::Routing::Routes.draw do |map|
map.resources :users
map.resources :clients, :has_one => :user
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
Controller
class UsersController < ApplicationController
before_filter :load_client
def new
@user = User.new
@client = Client.new
end
def load_client
@client = Client.find(params[:client_id])
end
def create
@user = User.new(params[:user])
@user.client_id = @client.id
if @user.save
flash[:notice] = "Credentials created"
render :new
else
flash[:error] = "Credentials created failed"
render :new
end
end
View
<% form_for @user, :url => client_user_url(@client) do |f| %>
<p>
<%= f.label :login, "Username" %>
<%= f.text_field :login %>
</p>
<p>
<%= f.label :password, "Password" %>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation, "Password Confirmation" %>
<%= f.password_field :password_confirmation %>
</p>
<%= f.submit "Create", :disable_with => 'Please Wait...' %>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的表单标记错误,您发布到
/users
时没有:client_id
。试试这个:
或者,您可以使用嵌套资源:
config/routes.rb
Controller
View
Your form tag is wrong, you are posting to
/users
without the:client_id
.Try this:
Alternatively, you could use nested resources:
config/routes.rb
Controller
View
我通过在创建客户端时使用嵌套属性(包括用户模型)解决了这个问题。而且它工作完美。
如果你们中的任何人需要更多信息,这里有两个截屏视频帮助我想出了解决方案:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/196-nested-model-form-part-2
I solved this by using nested attributes, by including the user model, when creating the client. And it works flawlessly.
In case any of you guys need more info here's the two screencasts that helped me come up with as solution:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/196-nested-model-form-part-2