【问题标题】:Redirect button to view in Ruby on Rails在 Ruby on Rails 中查看的重定向按钮
【发布时间】:2017-09-14 17:03:49
【问题描述】:

我有一个登录页面和一个注册页面。我希望在点击登录或注册按钮后,用户被重定向到另一个视图,这将是一个微博。

注册页面的代码如下。 (如表格部分) -

   <h2 class="text-center">Sign Up</h2>
                <%= form_for(input_output_SignUp_url) do |f| %> 
                <%= f.label :first_name,"First name:" %>
                <%= f.text_field :first_name %>

                <%= f.label :last_name,"Last name:" %>
                <%=f.text_field :last_name %>

                <%= f.label :email,"Email:" %>
                <%= f.email_field :email %>

                <%= f.label :phone,"Phone no:"%>
                <%= f.text_field :phone %>

                <%= f.label :city,"City:" %>
                <%= f.text_field :city %>

                <%= f.label :addr_1,"Address 1:" %>
                <%=f.text_field :addr_1 %>

                <%= f.label :addr_2,"Address 2:" %>
                <%= f.text_field :addr_2 %>

                <%= f.label :state,"State:"%>
                <%= f.text_field :state %>

                <%= f.label :postal_code,"Postal Code:"%>
                <%= f.text_field :postal_code %>

                <%= f.label :password,"Password:"%>
                <%= f.password_field :password %>

                <%= f.label :password_confirmation, "Confirmation:" %>
                <%= f.password_field :password_confirmation, class: 'form-control' %>

                <%= button_to "View profile", input_output_micropost_path%>
            <% end %>

现在这不起作用。当我点击按钮时,它显示一个错误 - No route matches [POST] "/input_output/SignUp"

路由文件如下:-

   root 'static_pages#home'

   get 'static_pages/home'
   get 'static_pages/genre'
   get 'static_pages/accessories'
   get 'static_pages/contactus'
   get 'static_pages/aboutus'

   get 'input_output/Login'
   get 'input_output/SignUp'
   get 'input_output/micropost'

   get '/genre', to: 'static_pages#genre' 
   get '/accessories', to: 'static_pages#accessories'
   get '/aboutus', to: 'static_pages#aboutus'
   get 'contactus', to: 'static_pages#contactus'

   get 'Home', to: 'static_pages#home'
   get '/Login', to: 'input_output#Login' 
   get '/micropost', to: 'input_output#micropost'
   get '/SignUp', to: 'input_output#SignUp'
end

我怎样才能让这个错误消失?

编辑:- 如所问,这是 rake routes 的结果 /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:51:警告:常量 ::Fixnum 已弃用

/home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:52:警告:常量:: Bignum 已弃用

/home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/core_ext/numeric/conversions.rb:138:警告: 常量 ::Fixnum 已弃用

rake 中止!

ArgumentError: Missing :controller key on routes definition,请检查您的路由。

/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/config/routes.rb:25:in `block in'

/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/config/routes.rb:1:in `'

/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/config/environment.rb:5:in `'

任务:TOP => 路线 => 环境

【问题讨论】:

  • 您要链接到哪个视图?请运行rake routes 并发布输出。
  • 您所有的路线都是“获取”请求。您需要添加一个路由,它将表单中的数据“发布”到您要处理它的控制器和操作。例如。发布“输入输出/注册”
  • @AlejandroMontilla 我想将它链接到名为“micropost”的视图,该视图是名为 InputOutput 的控制器的一部分,该控制器还包含注册页面和登录页面。
  • @bkunzi01 我不太确定“发布”路线的确切格式应该是什么。如上一条评论所述,我希望按钮指向的视图称为“微博”。那么我应该如何构建这个?
  • 控制器的名称和指向此视图的控制器的操作是什么?所有 Rails 视图都与一个控制器和该控制器中的一个动作相关联。告诉我们,我们可以为您提供路线。我强烈推荐一些关于 Rails 路由的教程。

标签: ruby-on-rails ruby model-view-controller routes


【解决方案1】:

这很简单。而不是

<%= button_to "View profile", input_output_micropost_path%>

提交

<%= f.submit "Sign up", class: "btn btn-default" %>

然后在您的控制器中的创建操作的末尾添加一个重定向。像这样的

def create
  @user = User.new(user_params)
  if @user.save
    redirect_to input_output_micropost_path, notice: 'User was successfully created.'
  else
    render :new
  end
end

编辑: 您的用户模型需要 RESTful 路由。如果你把它放在 input_output 命名空间下,它会是这样的

resources :input_outputs do
  resources :users
end

我推荐 Michael Hardt 的 Rails 教程。它非常详细地展示了如何做这样的事情。很经典。

【讨论】:

    【解决方案2】:

    你的路由的顶部是错误的,路由必须指向一个控制器和控制器内部的动作,所以路由应该是这样的:

    根到:'static_pages#home' 得到'/流派',到:'static_pages#genre' 获取'/accessories',到:'static_pages#accessories' 得到'/aboutus',到:'static_pages#aboutus' 获取“/contactus”,到:“static_pages#contactus” 获取'/Home',到:'static_pages#home' 获取“/登录”,到:“输入输出#登录” 得到'/micropost',到:'input_output#micropost' 得到'/SignUp',到:'input_output#SignUp' 获取'/Home',到:'static_pages#home' 获取“/登录”,到:“输入输出#登录” 得到'/micropost',到:'input_output#micropost' ################################################# #### 表单的操作设置为发布,所以注册 #### 必须是发布路线 ################################################# 将“/SignUp”发布到:“input_output#SignUp”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      相关资源
      最近更新 更多