如何在轨道中正确渲染局部4

时间:2022-05-21 20:16:15

I'm currently working on a remake of craigslist as a basic rails project. I have a login link on the homepage with an AJAX request to show a div containing the login partial.

我目前正在重新制作craigslist作为基本的rails项目。我在主页上有一个登录链接,带有一个AJAX请求,显示包含登录部分的div。

_login.html.erb

<%= form_for @user, url: { action: "/authenticate" } do |f| %>
  <%= f.label :email %>
  <%= f.text_field :email %><br>
  <%= f.label :password %>
  <%= f.text_field :password %>
  <%= f.submit "Log In!" %>
<%= flash[:notice] %>
<% end %>

categories_controller.rb aka my homepage action methods look like this:

categories_controller.rb又名我的主页操作方法如下所示:

  def index
    @user = User.all
    @categories = Category.all
  end

I'm getting the error: "No route matches {:action=>"/users_controller/authenticate", :controller=>"categories"}"

我收到错误:“没有路由匹配{:action =>”/ users_controller / authenticate“,:controller =>”categories“}”

How do I route my form to another action method in a separate controller? (i.e. users_controller)

如何在单独的控制器中将表单路由到另一个操作方法? (即users_controller)

3 个解决方案

#1


0  

Helpers

You'll be best reading up on rails path helpers - if set up correctly, these should point to the correct URL for you:

您最好阅读rails路径助手 - 如果设置正确,这些应指向您正确的URL:

#config/routes.rb
resources :users do
   collection do
      post :authenticate #-> domain.com/users/authenticate
   end
end

The problem you have is that you're calling { action: "/authentication" } - which basically isn't an action at all. You're trying to call a url, which won't work.

你遇到的问题是你正在调用{action:“/ authentication”} - 这基本上不是一个动作。你试图调用一个不起作用的网址。

You'll be best using the path helper as follows:

你最好使用路径助手,如下所示:

<%= form_for @user, user_authentication_path %>

#2


0  

If you want to submit your form for any particular controller, any particular action and want to pass some extra parameter (use action that define in controller that you pass on controller )

如果要为任何特定控制器提交表单,请执行任何特定操作并希望传递一些额外参数(使用在控制器中定义的操作在控制器上传递)

for example

<%= form_for @user, :url => {:controller => "users", :action => "authenticate"} do |f| %>

In the above code the form is submitted to that controller(that you pass on url) and goto that (you pass on action) action. it will take defaults to the current action.

在上面的代码中,表单被提交给该控制器(您传递url)并转到(传递操作)动作。它将默认为当前操作。

#3


0  

From your code, we have can modify it as below to get rid of the error you are getting by either by : url: {controller: "users", action: "/authenticate" } or using named route path for example: {url: users_path}.

从您的代码中,我们可以修改它,如下所示,通过以下方式消除您所获得的错误:url:{controller:“users”,action:“/ authenticate”}或使用命名路径路径,例如:{url :users_path}。

We can pass also pass parameters like url: {controller: "categories", action: "/authenticate", id: "SO", one: "stack" } or {url: users_path(id:'so')}. These parameters are send through URL to the controller method and we can use them in controller using params[:id] or params[:one].

我们也可以传递url:{controller:“categories”,action:“/ authenticate”,id:“SO”,one:“stack”}或{url:users_path(id:'so')}等传递参数。这些参数通过URL发送到控制器方法,我们可以使用params [:id]或params [:one]在控制器中使用它们。

you can see your routes using bundle exec rake routes or simply rake routes

你可以使用捆绑exec rake路线或简单的rake路线来查看你的路线

#1


0  

Helpers

You'll be best reading up on rails path helpers - if set up correctly, these should point to the correct URL for you:

您最好阅读rails路径助手 - 如果设置正确,这些应指向您正确的URL:

#config/routes.rb
resources :users do
   collection do
      post :authenticate #-> domain.com/users/authenticate
   end
end

The problem you have is that you're calling { action: "/authentication" } - which basically isn't an action at all. You're trying to call a url, which won't work.

你遇到的问题是你正在调用{action:“/ authentication”} - 这基本上不是一个动作。你试图调用一个不起作用的网址。

You'll be best using the path helper as follows:

你最好使用路径助手,如下所示:

<%= form_for @user, user_authentication_path %>

#2


0  

If you want to submit your form for any particular controller, any particular action and want to pass some extra parameter (use action that define in controller that you pass on controller )

如果要为任何特定控制器提交表单,请执行任何特定操作并希望传递一些额外参数(使用在控制器中定义的操作在控制器上传递)

for example

<%= form_for @user, :url => {:controller => "users", :action => "authenticate"} do |f| %>

In the above code the form is submitted to that controller(that you pass on url) and goto that (you pass on action) action. it will take defaults to the current action.

在上面的代码中,表单被提交给该控制器(您传递url)并转到(传递操作)动作。它将默认为当前操作。

#3


0  

From your code, we have can modify it as below to get rid of the error you are getting by either by : url: {controller: "users", action: "/authenticate" } or using named route path for example: {url: users_path}.

从您的代码中,我们可以修改它,如下所示,通过以下方式消除您所获得的错误:url:{controller:“users”,action:“/ authenticate”}或使用命名路径路径,例如:{url :users_path}。

We can pass also pass parameters like url: {controller: "categories", action: "/authenticate", id: "SO", one: "stack" } or {url: users_path(id:'so')}. These parameters are send through URL to the controller method and we can use them in controller using params[:id] or params[:one].

我们也可以传递url:{controller:“categories”,action:“/ authenticate”,id:“SO”,one:“stack”}或{url:users_path(id:'so')}等传递参数。这些参数通过URL发送到控制器方法,我们可以使用params [:id]或params [:one]在控制器中使用它们。

you can see your routes using bundle exec rake routes or simply rake routes

你可以使用捆绑exec rake路线或简单的rake路线来查看你的路线