将数据加载到form_tag中,而不刷新Rails 4中的页面

h

在Rails项目中,我有2个控制器:任务和产品。我想在views/tasks/_form.html.erb获取产品名称,form_tag然后从数据库中找到产品的数据,并通过javascript将产品的详细信息显示给用户,而无需重新加载页面。我有以下代码:

task_controller:

  class TasksController < ApplicationController
  before_action :set_task, only: [:show, :edit, :update, :destroy]

  before_filter :authenticate_user!
  layout "task"

  # GET /tasks
  # GET /tasks.json

  def show_product
    @product = Product.all
  end

  def index
    @tasks = Task.all
  end

  # GET /tasks/1
  # GET /tasks/1.json
  def show
  end

  # GET /tasks/new
  def new
    @list_of_category = Category.all
    @list_of_product = Product.all
    @reporter = Reporter.find(params[:reporter_id])
    @task = Task.new
  end

  # GET /tasks/1/edit
  def edit
    @list_of_category = Category.all
    @list_of_product = Product.all
  end

  # POST /tasks
  # POST /tasks.json
  def create
    @task = Task.new(task_params)
    @task.responsibility = current_user.responsibility
    @task.reporter = @reporter
    @task.date_time = Time.now
    @task.trace_code = (Time.now.to_f * 1000).to_i

    respond_to do |format|
      if @task.save
        format.html { redirect_to @task, notice: 'Task was successfully created.' }
        format.json { render action: 'show', status: :created, location: @task }
      else
        format.html { render action: 'new' }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /tasks/1
  # PATCH/PUT /tasks/1.json
  def update
    respond_to do |format|
      if @task.update(task_params)
        format.html { redirect_to @task, notice: 'Task was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /tasks/1
  # DELETE /tasks/1.json
  def destroy
    @task.destroy
    respond_to do |format|
      format.html { redirect_to tasks_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_task
      @task = Task.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def task_params
      params.require(:task).permit(:responsibility_id, :reporter_id, :date_time, :trace_code, :status, :describe, :category_id, :product_id)
    end
end

views / tasks / _form.html.erb

<%= form_tag 'tasks/show_product', :remote => true, :method => :get  do %>
    <%= text_field_tag :q %><br/>
    <%= submit_tag %>
<% end %>
<div id="response">

</div>

视图/任务/show_product.js.erb

$("#response").html("<%= escape_javascript(render 'tasks/show_product') %>")

views / tasks / _show_product.html.erb

<h1>Test</h1>

routes.rb

match "/tasks/show_product" => "tasks#show_product" , :via => :get

但是当我单击时submit_tag,产品名称不会发送form_tag,也不会在页面上进行任何更改。

我单击时的系统日志submit_tag

    Started GET "/tasks/show_product?utf8=%E2%9C%93&q=Product+1&commit=Save+changes" for 127.0.0.1 at 2014-05-04 13:35:36 +0430
Processing by TasksController#show as JS
  Parameters: {"utf8"=>"✓", "q"=>"Product 1", "commit"=>"Save changes", "id"=>"show_product"}
  Task Load (0.0ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = ? LIMIT 1  [["id", "show_product"]]
Completed 404 Not Found in 2ms

ActiveRecord::RecordNotFound (Couldn't find Task with id=show_product):
  app/controllers/tasks_controller.rb:85:in `set_task'


  Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
  Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
  Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
  Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (29.0ms)

问题出在哪儿?

理查德·派克(Richard Peck)

这是您的错误:

ActiveRecord::RecordNotFound (Couldn't find Task with id=show_product)

问题基本上是您正在尝试加载show操作。典型的原因是您文件中声明之后已经声明了路径resources :tasksroutes.rb

试试这个:

#config/routes.rb
resources :tasks do
    collection do
        get :show_products
    end
end

#app/views/tasks/_form.html.erb
<%= form_tag tasks_show_products_path, :remote => true, :method => :get  do %>

这将为您提供collection一条路线,然后您可以使用相应的路径助手来调用该路线。这应该可以工作,因为您当前的错误是由与show操作的冲突引起的

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

jQuery UI将页面加载到div中而不刷新

来自分类Dev

Rails form_tag提交数据而无需重新加载整个页面

来自分类Dev

将HTML页面加载到Rails服务器中

来自分类Dev

将页面数据加载到div中的最佳方法

来自分类Dev

如何设置方法放置在rails中的form_tag中?

来自分类Dev

更改rails form_tag中的accept-charset属性

来自分类Dev

Rails 4如何将图像作为Blob加载到数据库中

来自分类Dev

Rails 4如何将图像作为Blob加载到数据库中

来自分类Dev

rails 4 form_tag用法

来自分类Dev

Rails 4:在特定页面中禁用Turbolinks

来自分类Dev

我如何在form_tag rails4中使用重置按钮

来自分类Dev

如何在Rails 4中使用form_tag嵌套模型?

来自分类Dev

将HTML写入MVC 4中的页面

来自分类Dev

Ajax的页面滚动效果如何将数据加载到页面中

来自分类Dev

在Rails中使用form_tag将多个参数传递到当前页面

来自分类Dev

Rails:使用单选按钮将form_tag重定向到“显示”页面

来自分类Dev

将arrayList数据加载到JTable中

来自分类Dev

将数据帧加载到列表中

来自分类Dev

将数据加载到ExpandableListView中

来自分类Dev

将数据从SQL加载到R中

来自分类Dev

将数据加载到UITableView中

来自分类Dev

从文件将数据加载到表中

来自分类Dev

将数据逐个加载到 UICollectionView 中

来自分类Dev

OPENCART - 类别页面中的 4 个产品

来自分类Dev

将HTML ID添加到form_tag rails 4

来自分类Dev

如何通过脚本标签将数据(CSV文件或类似文件)加载到页面中?

来自分类Dev

将XHR数据加载到React Redux路由页面中时,事件的顺序是什么?

来自分类Dev

Wicket - 在页面呈现后将数据加载到表格中

来自分类Dev

离子 4 加载直到页面中的图像加载

Related 相关文章

热门标签

归档