简体   繁体   English

在[POST] [Ruby on Rails]上路由错误

[英]Routes error on [POST] [Ruby on Rails]

Right now I'm building a project management app in rails, here is some background info: 现在,我正在Rails中构建一个项目管理应用程序,这是一些背景信息:

Right now i have 2 models, one is User and the other one is Client. 现在我有2个模型,一个是User,另一个是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) 客户端和用户具有一对一的关系(客户端-> has_one和用户->属于_to,这意味着它在用户表中的外键)

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. 通过为他的ID为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} 我不断收到此错误:没有路由将{/ method =>:post}与“ / clients // user”匹配

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 %>

Your form tag is wrong, you are posting to /users without the :client_id . 您的表单标签是错误的,您要发布到/users而不带:client_id

Try this: 尝试这个:

<% form_for @user, :url => {:controller => 'users', :action => 'new', :client_id => @client.id} do |f| >

Alternatively, you could use nested resources: 另外,您可以使用嵌套资源:

config/routes.rb config / routes.rb

map.resources :clients do |clients|
  clients.resources :users
end

Controller 控制者

class UsersController < ApplicationController
  before_filter :load_client

  def load_client
    @client = Client.find(params[:client_id])
  end

  # Your stuff here
end

View 视图

<% form_for [@client, @user] do |f| %>

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-1

http://railscasts.com/episodes/196-nested-model-form-part-2 http://railscasts.com/episodes/196-nested-model-form-part-2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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