我想在show 页面上创建,但我的代码无效。
模型看起来像 - >用户
Gv$session
产品型号 - >
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :plan
has_one :profile
attr_accessor :stripe_card_token
def save_with_payment
if valid?
customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
self.stripe_customer_token = customer.id
save!
end
end
end
评论模型 - &gt;
class Product < ActiveRecord::Base
mount_uploader :image, ImageUploader
has_one :opsystem, class_name: "Opsystem",
foreign_key: "opsystem_id"
validates_presence_of :name, :price
validates_numericality_of :price
attr_accessor :name
has_many :comments, dependent: :destroy
def opsystem
end
end
产品控制器 - &gt;
class Comment < ActiveRecord::Base
attr_accessor :coment, :product_id, :user_id
belongs_to :product
belongs_to :user
end
评论控制器 - &gt;
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def index
@products = Product.all
@admin_user = User.find(3)
end
def show
@products = Product.all
@admin_user = User.find(3)
@opsystems = Opsystem.all
# users = User.where(name: 'Oscar')
# users.new.name # => 'Oscar'
@product = Product.find(params[:id])
@product_os = @product.opsystem_id
@opsystem = Opsystem.find(@product_os)
@comment = Comment.new
@comment.user = current_user
# @operatingsystems = Opsystem.find_by(id: @pr )
end
def new
@admin_user = User.find(3)
@opsystems = Opsystem.all
if current_user == @admin_user
@product = Product.new
else
redirect_to root_url
end
end
# GET /products/1/edit
def edit
@admin_user = User.find(3)
end
# POST /products
# POST /products.json
def create
@product = Product.new(product_params)
@opsystems = Opsystem.all
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :price, :image, :review, :processor, :ram_rom, :battery, :review, :display, :video, :camera, :opsystem_id )
end
end
在class CommentsController < ApplicationController
def create
@product = Product.find(params[:product_id])
@comment = @product.comments.build(params[:comment])
@comment.user = current_user
if @comment.save
redirect_to @product, notice: "Comment was created."
else
render :new
end
end
end
,来自产品,我已插入此表单:
products/show.html.erb
comments / _comments.html.erb包含 - &gt;
<div class="comment-wrapper">
<ul class="comments">
<%= render 'comments/comments' %>
<li>
<div class="comment-box-wrapper">
<%= form_for [@product, Comment.new], :remote => true do |f| %>
<div class="comment-textarea">
<%= f.text_area :coment %>
</div>
<div class="actions">
<%= f.submit "Comment", :class => "btn btn-small pull-right" %>
</div>
<% end %>
</div>
</li>
</ul>
</div>
我是这项技术的初学者,我担心在制作表格时可能会犯一些错误:
<% @product.comments.each do |comment| %>
<li>
<div class="comment-inner-wrapper">
<div class="comment-controls">
<% if comment.user == current_user %>
<%= link_to [@product, comment], :method => :delete, :confirm => "Are you sure?" do %>
<i class="icon-trash"></i>
<% end %>
<% end %>
</div>
<div class="comment-author-pic">
<%= link_to comment.user do %>
<%= image_tag comment.user.image.url(:thumb) %>
<% end %>
</div>
<div class="comment-author">
<%= link_to comment.user.username, comment.user %>
</div>
<div class="comment-content">
<p><%= comment.content %></p>
</div>
</div>
</li>
<% end %>
答案 0 :(得分:3)
您的节目视图中未定义product
变量。相反,您可以使用您在控制器中定义的@project
,并通过Rails传递给您的视图。
答案 1 :(得分:0)
您的评论form_for
来电有问题。这是它应该是什么样子:
<%= form_for [@product, Comment.new], url: new_comment_path(@product), :remote => true, html: { method: :post } do |f| %>
所做的更改:
url: new_comment_path(@product, Comment.new)
html: { method: :post }
选项这更接近您的评论工作所需。有解释的变化:
:url
选项允许您指定将表单发送到哪个url端点。您报告的jQuery错误表示该表单定位于/comments
路径,当您确实希望它定位/comments/new
路径时。使用路径助手获取新注释,可以将new_comment_path(@product)
作为正确的端点。
使用@product
模型对象,因为它从控制器传递到视图。这可能只是一个错字。
:html
选项允许您为表单元素的HTML生成提供其他选项。在这种情况下,我们告诉Rails生成POST
表单,因为Rails已经得出结论这是GET
形式。这将允许表单提交POST
到您的/comments/new
控制器,因为它应该是。
另外一个注意事项:您在评论迁移,评论模型以及表单中使用了`:coment&#39;中的拼写错误。如果这是项目的早期阶段,那么您将要纠正这一点,因为稍后处理拼写错误的字段名称变得乏味。
在products/show.html.erb
Comment.new
参数中移除form_for
,如下所示:
<%= form_for @product, url: new_comment_path(@product), :remote => true, html: { method: :post } do |f| %>
创建新模型对象时,该对象不应包含在路径中。