简体   繁体   English

Rails:如何在不更改视图的情况下调用控制器的create动作?

[英]Rails: How can I call a controller's create action without changing views?

I have a view, views/admin/index.html.erb , that has a button on it that I created with the following... 我有一个视图views/admin/index.html.erb ,上面有一个按钮,该按钮是用以下命令创建的...

views/admin/index.html.erb views / admin / index.html.erb

<%= button_to 'Manual Scrape', {:controller => "jobs", :action => "create", :job_type => "StatsAll"}, :method=>:post, :class => "btn btn-primary"  %>

controllers/jobs_controller.rb 控制器/jobs_controller.rb

  def create

    call_rake :import_records, :job_type => params[:job_type]
    flash[:notice] = "Import is executing..."

  end

controllers/application_controller.rb 控制器/application_controller.rb

  def call_rake(task, options = {})
    options[:rails_env] = Rails.env
    args = options.map { |n, v| "#{n.to_s.upcase}='#{v}'" }
    system "/usr/bin/rake #{task} #{args.join(' ')} --trace >> #{Rails.root}/log/rake.log &"
  end

When I click the button I would like it to remain on the current view which is, views/admin/index.html.erb , since I don't have a jobs view and don't believe I want one (maybe someone will convince me otherwise...). 当我单击该按钮时,我希望它保留在当前视图views/admin/index.html.erb ,因为我没有工作视图,也不相信自己想要一个(也许有人会说服否则我...)。 What I am trying to do is click the button and then on that view I will change the button to one that will call another action on the jobs_controller.rb to kill the job if necessary, otherwise it will execute until it completes. 我要执行的操作是单击按钮,然后在该视图上将按钮更改为一个按钮,该按钮将在jobs_controller.rb上调用另一项操作,以在必要时jobs_controller.rb该作业,否则它将执行直到完成。

However currently it tries to find the jobs view, which there is none. 但是,当前它尝试查找作业视图,没有该视图。 How can I correct this? 我该如何纠正?

Note: I've used some of the code from Railscast #127 to see how to run a background task. 注意:我已经使用了Railscast#127中的一些代码来查看如何运行后台任务。

You can always do this with some JavaScript/AJAX if you don't want to reload the page. 如果您不想重新加载页面,则始终可以使用一些JavaScript / AJAX进行此操作。 First and foremost, I assume you have a path for your jobs controller for the jobs function? 首先,我假设您具有作业功能的作业控制器路径? Find out by doing rake routes and see where your POST, not the GET for the create function in your jobs controller. 通过执行rake routes查找,并查看您的POST位置,而不是作业控制器中的create函数的GET。 I think you've made it this far to have it already there. 我认为您已经做到了这一点。

For your view, change it to: 对于您的视图,将其更改为:

<%= button_to 'Manual Scrape', jobs_create_path, :method=>:post, :remote => true, :class => "btn btn-primary"  %>

Your path may be different and want to take some kind of value, like the :job_type => "StatsAll" you had in there, but I'm not exactly sure. 您的路径可能有所不同,并且希望采用某种值,例如:job_type => "StatsAll" ,但我不确定。

You see that part for the :remote => true ? 您看到那部分的:remote => true吗? That is where the special AJAX stuff happens. 这就是发生特殊AJAX的地方。 If you want something more complicated though, it can be (but it's a ton more work). 如果您想要更复杂的东西,可以这样做(但是要多做很多工作)。 But this is the easy way for it to start going to create.js.erb. 但这是开始创建create.js.erb的简单方法。

Make a create.js.erb in your app/views/jobs for the button where you can escape_javascript to rerender the button (like you said you wanted to do). 在您的app / views / jobs中为按钮创建一个create.js.erb,您可以在其中escape_javascript重新呈现按钮(就像您说的那样)。 After it rerenders, you can also do this same process for the other feature you wanted to include: 重新渲染后,您还可以对要包含的其他功能执行相同的过程:

$('#create_button').html('<%= escape_javascript(render partial: "shared/create_job") %>'); // May need to use whatever locals you need to in here.

This should do it, since all it seems you want to do is just call a function and then stay on the page, but change the button. 应该这样做,因为您似乎只想调用一个函数,然后停留在页面上,但是更改按钮即可。

Also, for the above code, this assumes you're using a partial for the button. 另外,对于上面的代码,这假定您正在为按钮使用部分代码。 This is so you don't have to completely re-render/reload the page, just the partial that corresponds with the button. 这样一来,您不必完全重新渲染/重新加载页面,而只需要与按钮相对应的部分即可。

So I've found that I can specify which page to go to by just doing a redirect_to at the end of the action. 因此,我发现我可以通过在操作结束时执行redirect_to来指定要转到的页面。 Like so... 像这样

  def create
    # Do stuf...
    redirect_to 'admin/#page-i-was-at'
  end

You can use the following for your create action. 您可以将以下内容用于create操作。

def create    
  call_rake :import_records, :job_type => params[:job_type]
  redirect_to :back, notice: "Import is executing..."
end

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

相关问题 Rails:在不更改URL的情况下调用控制器操作/重定向到控制器索引 - Rails: call controller action without changing url / redirect to controller index 如何在Rails中使用对象的控制器的create动作用对象的新实例渲染局部对象? - How can I render a partial with a new instance of my object from the create action of that object's controller in Rails? 我可以在另一个动作中调用动作(在rails控制器中)吗? - Can I call an action in another action (in a rails controller)? 如何创建Rails控制器动作? - How do I create a rails controller action? 如何在不带URL控制器的情况下创建到永久链接的Rails路由? - How can I create a Rails route to a permalink - without the controller in the URL? Rails - 如何将创建/新操作从一个控制器放入另一个控制器的索引? - Rails - How can I put the create/new action from one controller into the index of another controller? 如何在不更改 rails 视图中的路由的情况下调用控制器方法 - How do I call a controller method without changing the route from the view in rails 如何将对rails控制器操作的调用静音 - How to silence the call to a rails controller's action all together Rails从另一个控制器调用“创建”操作 - Rails Call 'Create' Action from Another Controller Rails调用另一个控制器的动作 - Rails call another controller's action
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM