所以当我从我的ui按下我的rsvp按钮时,我收到此错误。也许我的路线有问题或我的控制器有问题。 我现在不太确定。我对rails非常陌生,而且我长期坚持这个问题。当我做一些更改时,会弹出另一个错误。我真的很感激一些帮助
class RsvpController < ApplicationController
def create
rsvp = current_user.rsvp.build({post_id: params[:id]})
if rsvp.save
end
end
end
发布控制器
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
@post = Post.find(params[:id])
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
unless current_user == @post.user
redirect_back fallback_location: root_path, notice: 'User is not owner'
end
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:date, :user_id, :description, :name, :address)
end
load_and_authorize_resource
def create
@rsvp=rsvp.new
end
end
=
的routes.rb
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resources :posts
devise_for :users
root 'home#index'
get 'home/ruby_meetup'
resources :posts do
post 'rsvp', to: 'rsvps#create', on: :member
end
此外,我想要显示/注册的用户数量,但我会在我的ui上显示随机数字和字母。我的节目页面有什么不对的吗?
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Date:</strong>
<%= @post.date %>
</p>
<p>
<strong>Name:</strong>
<%= @post.name %>
</p>
<p>
<strong>User_id:</strong>
<%= @post.user_id %>
</p>
<p><strong>Address:</strong> <%= @post.address %></p>
<p>
<strong>Description:</strong>
<%= @post.description %>
</p>
<p>
<strong>registered:</strong>
<%=@post.user %>
</p>
<% if current_user == @post.user %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%end%>
<%= link_to 'Back', posts_path %>
<div class="rsvp"><%= button_to "Rsvp now", rsvp_post_path(@post), class: "btn btn-primary" %></div>
<div class="map"><%= image_tag "http://maps.googleapis.com/maps/api/staticmap?center=#{@post.latitude},#{@post.longitude}&markers=#{@post.latitude},#{@post.longitude}&zoom=12&size=450x400&key=AIzaSyCKzKMEhSNgwSXf7WV71pHWgzdpMkPn8W4",
class: 'img-fluid img-rounded', alt: "#{@post} on the map"%>
</div>
答案 0 :(得分:0)
控制器类名称应该是RsvpsController当前的RsvpController,请注意s
后Rsvp
丢失了。
答案 1 :(得分:0)
将您的路线更改为:
post 'rsvp', to: 'rsvp#create', on: :member