没有路由匹配 {:action=> "create" , :controller=> "reviews" , :id=> "2" , :user_id=>#<配置文件 I



我正在研究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]

但是,正如我所看到的,它进入了个人资料,我正在,所以我不明白这里有什么问题。

语法有问题,试试这个

= form_for([@profile.user, @profile, @review], :url => user_profile_reviews_path(@profile.user, @profile)) do |f|

由于您的资源是嵌套的,因此您需要传递userprofile然后review作为form_for中的第一个参数

建议:看你的代码,你甚至不需要user_id,可以避免在路由中嵌套profilereviewuser

希望对您有所帮助!

好的,这非常适合解决所描述的缺少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

然后我看到,在我的个人资料#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

我现在终于可以转到个人资料了,有一个评论窗口,这很棒!不过我无法保存评论,因为出现了另一个错误。但那是不同的情况。非常感谢!

最新更新