ruby-on-rails Ruby on Rails中的表单有什么问题?

gcxthw6b  于 2023-01-22  发布在  Ruby
关注(0)|答案(1)|浏览(276)

我正在尝试实现一个祈祷请求和祈祷回应表单,就像一个带有评论的微帖子。祈祷请求会像预期的那样被创建和删除,但是当我尝试发布一个祈祷回应时,它只会重新加载页面,没有错误,也没有 Flink 说明发布成功。无论发布是否有效,都会发生这种行为(空白回复没有错误,只是重新加载了页面)当我使用rails控制台时,我能够成功地构建和保存祈祷回复,它显示在rails服务器上。我的表单有什么问题?根据服务器日志,我发现当我尝试发布祈祷回复时,它使用的是RequestsController而不是PrayerResponsesController。我该如何解决这个问题?
下面是我的服务器日志错误:

Started POST "/requests" for 127.0.0.1 at 2023-01-18 16:24:36 -0600
Processing by RequestsController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "content"=>"Prayer response...", "request_id"=>"150", "commit"=>"Respond in Prayer"}
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/helpers/sessions_helper.rb:18:in `current_user'
  CACHE User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/helpers/sessions_helper.rb:18:in `current_user'
Completed 400 Bad Request in 4ms (ActiveRecord: 0.1ms | Allocations: 2115)

  
ActionController::ParameterMissing (param is missing or the value is empty: request
Did you mean?  request_id):

下面是我的代码:
控制器/祷告响应控制器. rb

class PrayerResponsesController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy]
  before_action :correct_user, only: :destroy

  def create
    @prayer_response = current_user.prayer_responses.build(prayer_response_params)
    if @prayer_response.save
      flash[:success] = "Prayer Response created!"
      redirect_back(fallback_location: root_url)
    else
      render 'static_pages/requests', status: :unprocessable_entity
    end
  end

  def destroy
    @prayer_response.destroy
    flash[:success] = "Prayer Response deleted"
    redirect_back_or_to( root_url, status: :see_other )
  end

  private

    def prayer_response_params
      params.require(:prayer_response).permit(:content, :request_id)
    end

    def correct_user
      @prayer_response = current_user.prayer_responses.find_by(id: params[:id])
      redirect_to root_url, status: :see_other if @prayer_response.nil?
    end
end

视图/静态页面/请求. html. erb

<% provide(:title, "Prayer Requests") %>
<div class="row">
  <aside class="col-md-4">
    <section class="request-form">
      <%= render 'shared/request_form' %>
    </section>
  </aside>
  <div class="col-md-8">
    <h3>Prayer Requests</h3>
    <% if Request.all != nil %>
        <%= render Request.all %>
    <% end %>
  </div>
</div>

视图/请求/_请求. html. erb

<li id="request-<%= request.id %>" class="requests">
  <%= link_to gravatar_for(request.user, size: 50), request.user %>
  <span class="user"><%= link_to request.user.name, request.user %></span>
  <span class="content"><%= request.content %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(request.created_at) %> ago.
    <% if current_user?(request.user) %>
      <%= link_to "delete", request, data: { "turbo-method": :delete,
                                            "turbo-confirm": "Are you sure?"} %>
    <% end %>
  </span>
  <% if current_user != nil %>
  <span>
    <%= render 'shared/prayer_response_form', request_id: request.id %>
  </span>
  <span>
    <% if request.prayer_responses.any? %>
      <ol class="prayer_responses">
      <% request.prayer_responses.reverse.each do |prayer_response| %>
        <%= render prayer_response %>
      <% end %>
      </ol>
    <% end %>
  </span>
  <% end %>
</li>

浏览次数/祷告回应/祷告回应. html. erb

<li id="prayer_response-<%= prayer_response.id %>">
  <%= link_to gravatar_for(prayer_response.user, size: 30), prayer_response.user %>
  <span class="user"><%= link_to prayer_response.user.name, prayer_response.user %></span>
  <span class="content"><%= prayer_response.content %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(prayer_response.created_at) %> ago.
    <% if current_user?(prayer_response.user) %>
      <%= link_to "delete response", prayer_response, data: { "turbo-method": :delete,
                                            "turbo-confirm": "Are you sure?"} %>
    <% end %>
  </span>
</li>

查看/共享/祈祷响应表单. html. erb

<%= form_with(model: @prayer_response) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <center>
    <div class="field">
      <%= f.text_area(:content, placeholder: "Respond to this prayer request...") %>
    </div>
    <div><%= f.hidden_field :request_id, value: request_id %></div>
      <%= f.submit "Respond in Prayer", class: "btn btn-primary" %>
  </center>
<% end %>

模型/祈祷_回应. rb

class PrayerResponse < ApplicationRecord
  belongs_to :user
  belongs_to :request
  default_scope -> { order( created_at: :desc) }
  validates :user_id, presence: true
  validates :request_id, presence: true
  validates :content, presence: true, length: { maximum: 500 }
end

型号/请求. rb

class Request < ApplicationRecord
  belongs_to :user
  has_many :prayer_responses, dependent: :destroy
  default_scope -> { order(created_at: :desc) }
  validates :user_id, presence: true
  validates :content, presence: true, length: { maximum: 280 }
end

数据库/迁移/20230113172701创建祈祷者响应. rb

class CreatePrayerResponses < ActiveRecord::Migration[7.0]
  def change
    create_table :prayer_responses do |t|
      t.text :content
      t.references :user, null: false, foreign_key: true
      t.references :request, null: false, foreign_key: true

      t.timestamps
    end
    add_index :prayer_responses, [:user_id, :request_id, :created_at]
  end
end

数据库/迁移/20230107204925创建请求. rb

class CreateRequests < ActiveRecord::Migration[7.0]
  def change
    create_table :requests do |t|
      t.text :content
      t.references :user, null: false, foreign_key: true

      t.timestamps
    end
    add_index :requests, [:user_id, :created_at]
  end
end

配置/路由. rb

root   "static_pages#home"
  get    "/help",    to: "static_pages#help"
  get    "/about",   to: "static_pages#about"
  get    "/contact", to: "static_pages#contact"
  get    "/new_public_prayers", to: "static_pages#new_public_prayers"
  get    "/signup",  to: "users#new"
  get    "/login",   to: "sessions#new"
  post   "/login",   to: "sessions#create"
  delete "/logout",   to: "sessions#destroy"
  resources :users do
    member do
      get :following, :followers
    end
  end
  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]
  resources :prayers,             only: [:create, :destroy]
  resources :requests,            only: [:create, :destroy]
  resources :prayer_responses,    only: [:create, :destroy]
  resources :comments,            only: [:create, :destroy]
  resources :private_prayers,     only: [:create, :destroy]
  resources :relationships,       only: [:create, :destroy]
  get "/private_prayers", to: "static_pages#private_prayers"
  get "/prayers", to: "static_pages#home"
  get "/comments", to: "static_pages#home"
  get "/requests", to: "static_pages#requests"
  get "/prayer_responses", to: "static_pages#requests"
end
ar7v8xwq

ar7v8xwq1#

实际上,答案在static_pages_controller.rb文件中...我需要将@prayer_response = current_user.prayer_responses.build添加到requests页面的函数中...

class StaticPagesController < ApplicationController
  def home
    if logged_in?
      @prayer = current_user.prayers.build
      @comment = current_user.comments.build
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
  end

  def help
  end

  def about
  end

  def contact
  end

  def new_public_prayers
  end

  def private_prayers
    if logged_in?
      @private_prayer = current_user.private_prayers.build
    else
      redirect_to root_url
    end
  end

  def requests
    if logged_in?
      @request = current_user.requests.build
      @prayer_response = current_user.prayer_responses.build
    else
      redirect_to root_url
    end
  end

end

这确保了在建立新的祈祷React等时遵循了正确的控制器。

相关问题