如何链接以使用rails中的link_to标记创建操作

时间:2021-08-31 01:27:04

I tried to link to create action, and thought something like

我试图链接创建动作,并想到类似的东西

    <%= link_to "Create new user", {controller: 'users' , action: 'create'}, method: 'post'  %>

, but when I click the generated link nothing happens and the url changes to

,但是当我点击生成的链接时,没有任何反应,并且网址会更改为

    http://0.0.0.0:3000/users?method=POST+%2Fusers

which indicates that the request is handled as a GET request and not a POST, any ideas ?

这表明请求是作为GET请求处理而不是POST,任何想法?

here is the create action from the users controller

这是来自用户控制器的创建操作

        def create


        @user = User.new params[:user]

        if @user.save
            flash[:notice] = 'User has successfully been created.'
            redirect_to users_path
        else
            flash[:notice] = 'There was an error creating this user.'
            redirect_to :back
        end

end 

3 个解决方案

#1


0  

In rails: <%= link_to 'Create new user' , new_user_path %> , Check the Documentation for more info about routing.

在rails中:<%= link_to'创建新用户',new_user_path%>,查看文档以获取有关路由的更多信息。

#2


-1  

Try it with (not tested):

尝试使用(未测试):

<%= link_to "Create new user", {controller: 'users' , action: 'create', method: 'post'}  %>

#3


-1  

You should use the named paths as they are generated from your routes.

您应该使用从路由生成的命名路径。

<%= link_to "Create new user", users_path, method: "post" %>

You can find the names of these methods by running rake routes and looking at the first column. Append _path for local links and _url for full URLs.

您可以通过运行rake路径并查看第一列来查找这些方法的名称。为本地链接附加_path,为完整URL附加_url。

More on named routes here

更多关于命名路线的信息

#1


0  

In rails: <%= link_to 'Create new user' , new_user_path %> , Check the Documentation for more info about routing.

在rails中:<%= link_to'创建新用户',new_user_path%>,查看文档以获取有关路由的更多信息。

#2


-1  

Try it with (not tested):

尝试使用(未测试):

<%= link_to "Create new user", {controller: 'users' , action: 'create', method: 'post'}  %>

#3


-1  

You should use the named paths as they are generated from your routes.

您应该使用从路由生成的命名路径。

<%= link_to "Create new user", users_path, method: "post" %>

You can find the names of these methods by running rake routes and looking at the first column. Append _path for local links and _url for full URLs.

您可以通过运行rake路径并查看第一列来查找这些方法的名称。为本地链接附加_path,为完整URL附加_url。

More on named routes here

更多关于命名路线的信息