如何在index.html.erb
页面上创建评论而不访问其他页面(新增和显示)。即。我想创建评论并在一个页面上显示(index.html.erb
)。
控制器
def index
@comments = Comment.all
@users = User.all
@count = Comment.count
render :layout => false
end
# GET /comments/1
# GET /comments/1.json
def show
@coment = Comment.all
@users = User.all
end
# GET /comments/new
def new
@users = User.all
@comment = Comment.new
end
# GET /comments/1/edit
def edit
end
# POST /comments
# POST /comments.json
def create
@user = User.find(session[:user_id]) #whoever the logged in user is
@comment = @user.comments.build(comment_params)
respond_to do |format|
if @comment.save
format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
format.json { render action: 'index', status: :created, location: @comment }
else
format.html { render action: 'new' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
查看/ _form (它应该在index.html.erb
页面中)
<div id="container">
<%= form_for(@comment,:html => { class: 'niceform', :multipart =>true }) do |f| %>
<br><br>
<fieldset>
<legend>new comment</legend>
<dl>
<dt><label for="email">Comment:</label></dt>
<dd><%= f.text_area :text, size: "50x10" %></dd>
</dl>
</fieldset>
<fieldset class="action">
<input type="submit" name="submit" id="submit" value="Send comment" />
</fieldset>
<% end %>
</div>
答案 0 :(得分:0)
首先,您希望最终采用sirius ror中的方法来进行javascript调用。
我会让你前往可以将表格添加到索引页面的地方。
在控制器中
def index
@comment = Comment.new
@comments = Comment.all
@users = User.all
@count = Comment.count
render :layout => false
end
在index.html.erb文件中添加
<%= render 'form' %>
如果您希望它通过ajax工作,则需要将form_for调用更新为include:remote =&gt;真
然后,您可以创建文件&#34; create.js.erb&#34;并做你需要操纵页面的任何js东西
希望这能让你开始朝着正确的方向前进