小编Rah*_*hul的帖子

Rails:link_to调用控制器中的自定义方法

我希望用来link_to调用我的控制器中的方法.然而,由于一些奇怪的原因,该路线寻找该show方法.

在我看来:

<% @beverages.each do |beverage| %>
    ..
    <%= link_to 'Archive', beverages_archive_path(:id => beverage.id) %>
    ..
<% end %>
Run Code Online (Sandbox Code Playgroud)

在我的config/routes.rb中

match 'beverages/archive' => 'beverages#archive'
Run Code Online (Sandbox Code Playgroud)

在我的beverages_controller.rb中

  def archive
    beverage = Beverage.find(params[:id])

    respond_to do |format|
    #  format.html # show.html.erb
      format.json { render json: beverage }
    end
    # beverage.update_attribute('archive', true)
  end
Run Code Online (Sandbox Code Playgroud)

当我单击视图中的存档链接时,URL确实更改为:http://localhost:3000/beverages/archive?id=11,但是我收到以下错误.

我得到的错误: 错误 ActiveRecord :: RecordNotFound(无法找到带有id = archive的Beverage):app/controllers/beverages_controller.rb:46:在`show'中

对我做错了什么的想法?非常感谢您的帮助!

PS.我还看了Rails 3 link_to删除destory方法调用show方法? 但似乎没什么用.

ruby ruby-on-rails

10
推荐指数
2
解决办法
2万
查看次数

Rails js.erb文件未被执行

我只是想让我的index.js.erb文件执行一个警告("hi")命令,但它无法正常工作.我对rails非常陌生,我想知道你们是否可以帮助我!看起来servers_controller.rb索引方法没有正确执行format.js.有什么建议/想法吗?

servers_controller.rb

def index
    @servers = Server.all

    update_all_servers #calls the method update_all_servers in application_controller.rb

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @servers }
      format.js #index.js.erb
    end
end
Run Code Online (Sandbox Code Playgroud)

index.js.erb的

alert("hi");
$("#comments").fadeOut();
Run Code Online (Sandbox Code Playgroud)

的index.html

<%- model_class = Server.new.class -%>
<div class="page-header">
  <h1><%=t '.title', :default => model_class.model_name.human.pluralize %></h1>
</div>
<table class="table table-striped">
  <thead>
    <tr>
 <!--     <th><%= model_class.human_attribute_name(:id) %></th> -->
      <th><%= model_class.human_attribute_name(:hostname) %></th>
      <th><%= model_class.human_attribute_name(:port) %></th>
  <!--    <th><%= model_class.human_attribute_name(:username) %></th>
      <th><%= model_class.human_attribute_name(:password) %></th>
      <th><%= model_class.human_attribute_name(:ssh_username) %></th>
      <th><%= model_class.human_attribute_name(:ssh_password) %></th> 
      <th><%= model_class.human_attribute_name(:source_branch) %></th> …
Run Code Online (Sandbox Code Playgroud)

javascript ruby ajax ruby-on-rails

8
推荐指数
1
解决办法
5304
查看次数

Gem如何在Controller.rb文件中运行特定方法

我希望每分钟在我的controller.rb文件中运行一个特定的方法.我正在寻找使用随时随地的宝石铁路,但我有点困惑如何做到这一点.

目前在schedule.rb我有:

every 1.minutes do 
runner "Server.update_all_servers"
end
Run Code Online (Sandbox Code Playgroud)

我不确定runner命令的作用.有人可以解释这个命令究竟是做什么的吗 根据我的理解,它调用Model.ModelMethod但我需要在application_controller.rb中调用一个名为update_all_servers()的方法.是否有可能做到这一点?或者我是否必须将application_controller.rb中的任何内容移动到模型文件(例如位于/models/server.rb中的文件)?

ruby cron ruby-on-rails whenever

3
推荐指数
1
解决办法
4474
查看次数

Rails自定义路由变得困惑:id

我正在尝试设置自定义路线.但是每当我点击beverage_locations/new页面时,它都会尝试在url中发送'new'作为索引路径中的:location_id.

route.rb

  controller 'beverage_locations' do
     get 'beverage_locations/:location_id' => 'beverage_locations#index'
     get 'beverage_locations/new' => 'beverage_locations#new'
  end
Run Code Online (Sandbox Code Playgroud)

错误

ActiveRecord::RecordNotFound in BeverageLocationsController#index

Couldn't find Location with id=new
Run Code Online (Sandbox Code Playgroud)

任何想法如何解决这个问题?

谢谢!

ruby-on-rails

0
推荐指数
1
解决办法
421
查看次数

标签 统计

ruby-on-rails ×4

ruby ×3

ajax ×1

cron ×1

javascript ×1

whenever ×1