我正在开发一个具有问答格式的项目,其中包含问题、答案和问题评论,但我对这种结构有疑问。我的路线图是这样的:
经验简介 --> 讨论
因此,在这个项目中,我有一个包含一些信息的体验资料和一个链接,用于汇总所有问答结构的讨论。我的问题是,当我尝试通过 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>