简体   繁体   English

连接路线,视图和控制器(导轨)

[英]Connecting routes, views and controllers (Rails)

I'm learning Rails so please pardon my amateur mistakes, but I've been stuck for about an hour or two and have made negative progress. 我正在学习Rails,所以请原谅我的业余错误,但是我被困了大约一两个小时,并且取得了负面的进步。

Goal: 目标:

From the user profile view, link to a form that allows this user to change their email. 在用户profile视图中,链接到一个允许该user更改其电子邮件的表单。 Once the form is submitted, it should trigger an appropriate method within the user controller . 提交表单后,它应在user controller内触发适当的方法。

I can handle the rest, I just haven't managed to connect the parts mentioned above. 我可以处理其余的事情,只是我还没有设法连接上述部件。 I have been reading railsTutorial.org and guides.rubyonrails.org but haven't been able to understand routing form_for() sufficiently. 我一直在阅读railsTutorial.orgguides.rubyonrails.org,但还不能充分理解routing form_for()

User Profile Link: 用户个人资料链接:

<%= @user.email %> <%= link_to "(change)", email_path(@user) %>

Routes 路线

Rails.application.routes.draw do
  get    'email'    => 'users#email_form'
  post   'email'    => 'users#changeemail'
end

User Controller 用户控制器

class UsersController < ApplicationController
  def email_form
    @user = User.find(params[:id])
  end

  def changeemail
    @user = User.find(params[:id])
    redirect_to @user
  end
end

Currently the error I get once I click the link is Couldn't find User with 'id'= which I assume means user ID is nil because I fail at passing it. 目前,我单击链接后遇到的错误是“ Couldn't find User with 'id'= ,我认为这意味着用户ID为零,因为我无法通过它。

I would greatly appreciate an explanation of what data is being passed through this workflow so I can be a better developer, thank you very much! 非常感谢您解释通过此工作流传递哪些数据,因此我可以成为一名更好的开发人员,非常感谢!

EDIT: 编辑:

The form itself: 表格本身:

<%= form_for(@user, url: user_path(@user))  do |f| %>
  <%= render 'shared/error_messages' %>

  <%= f.label :new_email %>
  <%= f.text_field :new_email, class: 'form-control' %>

  <%= f.label :password %>
  <%= f.password_field :password, class: 'form-control' %>

  <%= f.submit "Submit New Email", class: "btn btn-primary" %>
<% end %>

You could do this (note :id and "as"): 您可以这样做(注意:id和“ as”):

Rails.application.routes.draw do
  get    'email/:id'    => 'users#email_form', as :email
  post   'email/:id'    => 'users#changeemail', as :change_email
end

The :id is then expected to be part of the route. 然后,:id将成为路由的一部分。

Alternatively, pass the id directly when generating the url: 或者,在生成网址时直接传递ID:

<%= @user.email %> <%= link_to "(change)", email_path(id: @user) %>

This will make a call to "UsersController#update" 这将调用“ UsersController#update”

<%= form_for(@user, url: user_path(@user))  do |f| %>

...instead you would use something like:: ...相反,您将使用类似:

<%= form_for(@user, url: change_email_path(@user), method: :put)  do |f| %>

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for


...but in terms of best practices, if you want to do separate flow for email updating, you could be more explicit in treating it as a different resource (even though it's still the user record). ...但是就最佳做法而言,如果您要进行单独的电子邮件更新流程,则可以将其视为不同的资源(即使仍然是用户记录)也更加明确。

For example, you could map these to an explicit 'resource' with a #show and #update action... 例如,您可以使用#show和#update动作将它们映射到显式的“资源” ...

Routes: 路线:

resources :user_emails, only: [:show, :update]

Controller: 控制器:

class UserEmailsController < ApplicationController
  def show
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    redirect_to @user # goes back to UsersController#show
  end
end

Then the route would be: 那么路由将是:

<%= @user.email %> <%= link_to "(change)", user_email_path(@user) %>

In this case we don't have to say (id: @user) since the 'resource' generates the right urls for you. 在这种情况下,我们不必说(id:@user),因为“资源”会为您生成正确的网址。

...and this would be ...这将是

<%= form_for(@user, url: user_email_path(@user), method: :post)  do |f| %>

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

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