我正在开发一个具有Q& A格式的项目,包括问题,答案和问题评论,我遇到了这个结构的问题。我的路线图是:
体验资料 - >讨论
所以,在这个项目中,我有一个经验简介,其中包含一些信息和一个link_to 汇总所有Q& A结构的讨论。我的问题是,当我尝试通过link_to访问讨论结构(Q& A)时,没有任何内容呈现。我正在使用Rails 3.2.0和Ruby 1.9.3版本。我希望你的帮助!
体验资料(link_to):
<%= link_to "New topic", experience_discussions_path(@experience), :remote => true %></p>
讨论控制器:
respond_to :html, :json
def index
@experience = Experience.find(params[:experience_id])
@question = Question.new
@questions = Question.where(:experience_id => @experience.id).order('updated_at desc')
respond_with(@experience, @questions) do |format|
format.html { render :layout => !request.xhr? }
end
end
def create
@experience = Experience.find(params[:experience_id])
@question = @experience.questions.new(params[:question])
@question.user = current_user
if @question.save
flash[:notice] = 'Pergunta criada com sucesso'
else
respond_with(@experience, @question) do |format|
format.html { redirect_to experience_path(@experience)+'#discussion' }
end
end
讨论视图(索引):
<div id="questions">
<%= form_for(@question, :url => experience_discussions_path, :method => 'post') do |f| %><br />
<fieldset class ="new_question">
<div class="field">
<%= f.label "Ask question" %>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.submit "Create", :class => 'button' %>
</div>
</fieldset>
<% end %>
<% @questions.each do |q| %>
<h3 id="q_title">
<%= (:question) %>: <%= q.title %><br />
<small> <%= (:question_from) %> <%= q.user %></small>
</h3>
<%= form_for(Answer.new(:question => q),:url => experience_discussion_answers_path(@experience, q),:method => 'post') do |f| %>
<fieldset class="new_answer">
<div class="field">
<%= f.label "Resposta" %>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.submit "Create", :class => 'button' %>
</div>
</fieldset>
<% end if current_user == @experience.user && q.answers.empty? %>
<% q.answers.each do |answer|%>
<p><%= answer.body %></p>
<% end %>
<% q.comments.each do |comment|%>
<p <%= 'class=owner' if comment.user == @experience.user%> >
<small><%= user_link comment.user %></small>
<br>
<%= comment.body %>
</p>
<% end unless q.answers.empty? %>
<%= link_to("Add comment"), experience_discussion_comments_path(@experience, q)) %>
<%= render(:partial => 'question_comment') %>
<% end %>
</div>
答案 0 :(得分:0)
删除所有那些respond_to和respond_with代码并让rails显示一些错误,你会看到那里缺少什么,我猜你需要对你的方法response_to format.js并且也有.js.erb视图
检查日志并查看到达您服务器的请求的格式