- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在一个 Rails 项目中,我有 2 个 Controller :任务和产品。我想在 form_tag
中的 views/tasks/_form.html.erb
中获取产品名称,并从数据库中找到产品的数据,并通过 javascript 向用户显示产品的详细信息无需重新加载页面。我有以下代码:
任务 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
View /任务/_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>
views/tasks/show_product.js.erb
$("#response").html("<%= escape_javascript(render 'tasks/show_product') %>")
views/tasks/_show_product.html.erb
<h1>Test</h1>
路线.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)
问题出在哪里?
最佳答案
这是你的错误:
ActiveRecord::RecordNotFound (Couldn't find Task with id=show_product)
问题基本上是您正在尝试加载 show
操作。典型的原因是您在routes.rb
文件
resources :tasks
之后声明了路径
试试这个:
#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
route ,然后您可以使用相应的路径助手调用它。这应该有效,因为您当前的错误是由与 show
操作
关于ruby-on-rails - 在 form_tag 中加载数据而不刷新 Rails 4 中的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23454104/
我正在开发一个需要能够平均三个数字的 Facebook 应用程序。但是,它总是返回 0 作为答案。这是我的代码: $y = 100; $n = 250; $m = 300; $number = ($y
我只是无法弄清楚这一点,也找不到任何对我来说有意义的类似问题。我的问题:我从数据库中提取记录,并在我的网页上以每个面板 12 条的倍数显示它们。因此,我需要知道有多少个面板可以使用 JavaScrip
我是一名优秀的程序员,十分优秀!