我正在处理user_profile_reviews并被卡住。我现在有3个模型,但我知道,为配置文件创建单独的模型并不是一个好主意,但是由于我的所有路线都依赖于此结构,因此也意味着所有视图中的链接,我决定不更改它。
让您更清楚地了解:
Rails.application.routes.draw do
devise_for :user, controllers: { omniauth_callbacks: 'users/omniauth_callbacks', registrations: "users/registrations" }
resources :users do
resources :profiles do
resources :reviews, only: [:new, :create]
end
end
root 'home#index'
end
这是我的控制器:
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
def index
@profiles = Profile.all
end
def show
@user = User.find(params[:user_id])
@profile = Profile.find(params[:id])
@reviews = Review.where("profile_id = ?", params[:id])
end
def new
@user = User.find(params[:user_id])
end
def edit
@profile = Profile.find(params[:id])
end
def create
@profile = current_user.build_profile(profile_params)
respond_to do |format|
if @profile.save
format.html { redirect_to user_profile_path(current_user.id, current_user.profile.id), notice: 'Profile was successfully created.' }
format.json { render :show, status: :created, location: @profile }
else
format.html { render :new }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to user_profile_path(current_user.id, current_user.profile.id), notice: 'Profile was successfully updated.' }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url, notice: 'Profile was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_profile
@profile = Profile.find(params[:id])
end
def profile_params
params.permit(:about, :rating, :avatar)
end
end
评论
class ReviewsController < ApplicationController
before_filter :set_profile
before_filter :set_review, only: [:new, :create]
def new
@review = Review.new
end
def create
@profile = Profile.find(params[:profile_id])
@review = @profile.reviews.build(review_params)
@review.user_id = current_user.id
if @review.save
redirect_to @profile
else
redirect_to @profile, notice: "Error saving"
end
end
private
def review_params
params.permit(:content, :rating)
end
def set_pfofile
@profile = Profile.find(params[:profile_id])
end
def set_review
@profile = Profile.find(params[:id])
end
end
所以现在,我正在尝试创建评论表单,然后将其呈现在Profiles#show中,并解决上面的错误。
<div class="submit-review">
<%= form_for [@review, :url => user_profile_reviews_path(@profile)] do |f| %>
<label for="review">How was your experience?</label><br>
<%= f.label :rating %>
<%= f.select :rating, options_for_select([["Please select one", ""], 5, 4, 3, 2, 1]) %>
<%= f.input :content, placeholder:"Please enter your feedback here" %>
<%= f.submit "Submit your review", class: "btn btn-default" %> <br><br>
<% end %>
Showing ... /_form.html.erb where line #2 raised:
No route matches {:action=>"create", :controller=>"reviews", :id=>"2", :user_id=>#<Profile id: 2, about: "lena", rating: 3, created_at: "2019-11-22 21:27:03", updated_at: "2019-11-22 21:27:03", user_id: 2>}, missing required keys: [:profile_id]
但是,正如我所看到的,它已经进入个人资料了,所以我不明白这里出了什么问题。
答案 0 :(得分:1)
语法有误,请尝试
= form_for([@profile.user, @profile, @review], :url => user_profile_reviews_path(@profile.user, @profile)) do |f|
由于资源是嵌套的,因此需要传递user
,profile
,然后传递review
作为form_for
中的第一个参数
建议:查看您的代码,甚至不需要user_id
,可以避免将profile
和review
嵌套在user
下在路线上。
希望有帮助!
答案 1 :(得分:1)
好的,这非常适合解决ID缺失的问题。
form_for([@profile.user, @profile, @review], :url => user_profile_reviews_path(@profile.user, @profile)) do |f|
我遇到了另一个错误:
First argument in form cannot contain nil or be empty
然后我看到,在我的Profile#show中,我没有定义@review。仅评论。所以我这样做:
def show
@user = User.find(params[:user_id])
@profile = Profile.find(params[:id])
@review = Review.new
@reviews = Review.where("profile_id = ?", params[:id])
end
我现在终于可以进入个人资料,并且有一个评论窗口,这太好了!我无法保存评论,因为出现了另一个错误。但这是不同的情况。非常感谢!