ruby-on-rails - 如何为新模板添加路由?

标签 ruby-on-rails ruby ruby-on-rails-4

我是 Ruby 和 Rails 的新手,对为新模板渲染和添加路由有点困惑。

我关注 link_to标签

<td colspan="3">
 <%= link_to 'Show Current State', simulation, :action => :current_state, :class => 'btn btn-primary'%>
</td>

在哪里simulation是 Controller 的名称,action 是 SimulationController 中方法的名称.

我在我的 routes.rb 中添加了这个

  resources :simulations, except: [:edit]

  resources :simulations do
    collection do
     get 'current_state'
     post 'current_state'
   end
 end

在我的 SimulationController我添加了一个新方法的类,即

  def current_state
   byebug
  end

我的问题?路线没有重定向到 current_state方法。相反,它重定向到 http://localhost:3000/simulations/{someID}

此重定向正在调用 show行动。

def show
 ...
end

我怎样才能解决这个问题并制作<%= @simulation.dat %>可在 new.html.erb 中访问的行. new.html.erb的位置在以下路径中

views/simulations/index.html.js
views/similations/show.html.js
views/simulations/new.html.erb

这可能是一个基本问题,但我是 Rails 4 的新手。在此先感谢。

编辑-1

定义 get_state controller 中的方法

 def get_state
  @simulation = current_user.simulations.find(params[:id])
  return not_found if @simulation.nil?  
  .....
  /// How to send `@simulation` into `state.html.erb` formally as `new.html.erb`
end

最佳答案

您的代码中有太多遗漏。

首先,你不需要 2 resources :simulations , 只需将它们合并为一个:

resources :simulations, except: :edit do
  member do
    get 'current_state', action: 'get_state'
    post 'current_state', action: 'change_state'
  end
end

注意原来的collection block 更改为 member阻止。
collection之间的区别 block 和一个 member block 是您需要为 member 中的每个路由提供一个资源 ID block ,而 collection 中的那些不需要资源 ID block 。

另请注意,我添加了 action: 'xxx'在每条 route ,因此您必须在 SimulationsController 中添加这两个操作,一个用于 GET 请求,另一个用于 POST 请求。

更新

在这两个操作中,添加 render 'new'在最后。

更新结束

运行 rake routes在您的控制台(或 bundle exec rake routes 如果您安装了多个版本的 rails),您将看到所有路由以及列出的 url 辅助方法,如下所示:

                   Prefix Verb URI Pattern                    Controller#Action
current_state_simulations GET  /simulations/:id/current_state simulations#get_state
current_state_simulations POST /simulations/:id/current_state simulations#change_state
...

根据Prefix列, View 中的链接应该是

<%= link_to 'Show Current State', current_state_simulations_path(simulation), :class => 'btn btn-primary'%>

简而言之

<%= link_to 'Show Current State', [:current_state, simulation], :class => 'btn btn-primary'%>

Edit-1 更新

不要return在行动中,因为 return不会停止渲染。
相反,使用 raise ActionController::RoutingError.new('Not Found')将用户重定向到 404 页面。

您可以在ApplicationController 中定义实例方法:

class ApplicationController < ActionController::Base
  private
  def not_found!
    raise ActionController::RoutingError.new('Not Found')
  end
end

并修改你的SimulationsController :

def get_state
  @simulation = current_user.simulations.find(params[:id])
  not_found! unless @simulation
  # ...
  render 'new'
end

最佳实践
对于动态页面 Web 应用程序,不要呈现非 GET 请求的 View !

为什么?因为如果用户将一些数据发布到您的 Web 应用程序,然后刷新他/她的浏览器,该请求将再次发布,并且您的数据库会受到污染。 PATCH、PUT 和 DELETE 请求也是如此。

如果非 GET 请求成功,您可以将用户重定向到 GET 路径,如果非 GET 请求失败,则可以重定向到 400 页面。

关于ruby-on-rails - 如何为新模板添加路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31225772/

相关文章:

ruby-on-rails - bundle 安装在 Rails 应用程序中创建 'parallel' 文件夹

ruby-on-rails - 将页面预加载到 turbolinks 转换缓存中

mysql - 何时在 Rails 中的表中添加哪些索引

javascript - Ruby/Sinatra 的 JSON 数据库模块? Ruby/Sinatra 的 JS lowDB 替代品?

ruby - 如何部署基于 Ruby Gem 的服务器

ruby-on-rails - 将 Redis 与 Ruby on Rails 结合使用

mysql - 如何生成 ActiveRecord 查询

ruby-on-rails - 在构造 SQL 查询时,ActiveRecord 是否处理按值查找 `enum` ?

ruby-on-rails - 在测试中找不到 Rails 路由

ruby-on-rails - Rails ActiveRecord Model.uniq.pluck :id doesn't work with postgres