我正在尝试使用ajax在一个模拟个人资料页面中添加一个“推荐”部分。当我单击coding_skills索引页面上的推荐按钮时,我没有收到任何服务器或日志错误,但是页面停留在索引页面上,并且不加载“推荐”页面。任何建议都将不胜感激!
推荐控制器
class RecommendationsController < ApplicationController
def index
@recommendations = Recommendation.all
end
def new
@recommendation = Recommendation.new
end
def create
@recommendation = Recommendation.new(recommendation_params)
@recommendation.recommendation_author = current_user.email
if @recommendation.save
respond_to do |format|
format.html { redirect_to coding_skills_url }
format.js
end
end
end
def destroy
end
private
def recommendation_params
params.require(:recommendation).permit(:recommendation_content)
end
end
路线
Rails.application.routes.draw do
root :to => "coding_skills#index"
devise_for :users
resources :posts
resources :coding_skills do
resources :projects
end
resources :recommendations
end
Views#index
<%= render "best_of" %>
_Best_of.html.erb
<div id="best_of">
<h1>Recommendations for Cody</h1>
<p>Add a recommendation below!</p>
<% if current_user %>
<%= link_to "Add a recommendation", new_recommendation_path, class: "btn btn-success", remote: true %>
<% else %>
<p><%= link_to "Sign in to add a recommendation", new_user_session_path %></p>
<% end %>
<h3>Recommendations</h3>
<div id="recommendations">
<% if @recommendations.any? %>
<%= render(@recommendations) %>
<% end %>
</div>
<%= link_to "Close", root_path, id: "hide_best_of", class: "btn btn-success" %>
</div>
_form.html.erb
<div id="recommendation-header">
<h3>Hey there, <%= current_user.email %>!</h3>
</div>
<%= bootstrap_form_for @recommendation, remote: true do |f| %>
<%= f.text_area :recommendation_content, label: "What do you like about Cody?" %>
<%= f.submit "Add recommendation", class: "btn btn-success" %>
<% end %>
瑞克路由
Prefix Verb URI Pattern Controller#Action
root GET / coding_skills#index
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
coding_skill_projects GET /coding_skills/:coding_skill_id/projects(.:format) projects#index
POST /coding_skills/:coding_skill_id/projects(.:format) projects#create
new_coding_skill_project GET /coding_skills/:coding_skill_id/projects/new(.:format) projects#new
edit_coding_skill_project GET /coding_skills/:coding_skill_id/projects/:id/edit(.:format) projects#edit
coding_skill_project GET /coding_skills/:coding_skill_id/projects/:id(.:format) projects#show
PATCH /coding_skills/:coding_skill_id/projects/:id(.:format) projects#update
PUT /coding_skills/:coding_skill_id/projects/:id(.:format) projects#update
DELETE /coding_skills/:coding_skill_id/projects/:id(.:format) projects#destroy
coding_skills GET /coding_skills(.:format) coding_skills#index
POST /coding_skills(.:format) coding_skills#create
new_coding_skill GET /coding_skills/new(.:format) coding_skills#new
edit_coding_skill GET /coding_skills/:id/edit(.:format) coding_skills#edit
coding_skill GET /coding_skills/:id(.:format) coding_skills#show
PATCH /coding_skills/:id(.:format) coding_skills#update
PUT /coding_skills/:id(.:format) coding_skills#update
DELETE /coding_skills/:id(.:format) coding_skills#destroy
recommendations GET /recommendations(.:format) recommendations#index
POST /recommendations(.:format) recommendations#create
new_recommendation GET /recommendations/new(.:format) recommendations#new
edit_recommendation GET /recommendations/:id/edit(.:format) recommendations#edit
recommendation GET /recommendations/:id(.:format) recommendations#show
PATCH /recommendations/:id(.:format) recommendations#update
PUT /recommendations/:id(.:format) recommendations#update
DELETE /recommendations/:id(.:format) recommendations#destroy
发布于 2015-07-27 12:22:50
那么,您缺少类似如下所示的create.js.erb文件:
$('#element_id').html("<%= escape_javascript(render 'your_partial') %>");
element_id是您想要在页面上替换的html选择器。这取决于你的html的外观,以及在ajax调用过程中你想要替换它的哪一部分。
your_partial是您未提供的部分。
我想你可以用"j“来代替escape_javascript。
发布于 2015-07-27 12:27:06
我认为问题就在这一点上:
<%= link_to "Add a recommendation", new_recommendation_path, class: "btn btn-success", remote: true %>
末尾的remote: true
导致HTTP请求为sent as an AJAX request, with the Accept
header requesting a JavaScript response。
要解决此问题,请尝试删除remote: true
。那么应该遵循您的重定向。
https://stackoverflow.com/questions/31644770
复制相似问题