【发布时间】:2020-10-23 19:01:35
【问题描述】:
我的问题
当我在用户页面上单击提交表单时,我在两个不同的页面(意见和用户)上有两个表格,它会将我重定向到意见页面,我知道我可以重定向到我重定向的同一页面在操作中创建但有没有办法将用户表单重定向到用户页面
用户控制器
class UsersController < ApplicationController
include TheUser
before_action :set_user
before_action :authenticate_user!
before_action :user_signed_in?
def index
@users = User.all
@mutual_friends = User.where(id: show_two_friends)
end
def show
@user = User.find(params[:id])
@new_opinion = Opinion.new
respond_to do |format|
if @user
format.html
format.js
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
format.js
end
end
end
private
def show_mutual_friends
@ids = []
current_user.friends.each do |person|
person.friends.each do |m|
@ids << m.id
end
end
@ids.reject { |x| x == current_user.id }
end
def show_two_friends
show_mutual_friends.sample(2)
end
end
意见控制器
class OpinionsController < ApplicationController
include TheUser
before_action :set_user
before_action :set_opinion, only: [:show, :edit, :update, :destroy]
# GET /opinions
# GET /opinions.json
def index
@opinions = Opinion.all.order("created_at DESC")
@opinion = Opinion.new
end
# POST /opinions
# POST /opinions.json
def create
@opinion = @user.opinions.build(opinion_params)
respond_to do |format|
if @opinion.save
format.html { redirect_to opinions_url, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @opinion }
else
format.html { render :new }
format.json { render json: @opinion.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /opinions/1
# PATCH/PUT /opinions/1.json
def update
respond_to do |format|
if @opinion.update(opinion_params)
format.html { redirect_to @opinion, notice: 'Opinion was successfully updated.' }
format.json { render :show, status: :ok, location: @opinion }
else
format.html { render :edit }
format.json { render json: @opinion.errors, status: :unprocessable_entity }
end
end
end
# DELETE /opinions/1
# DELETE /opinions/1.json
def destroy
@opinion.destroy
respond_to do |format|
format.html { redirect_to opinions_url, notice: 'Opinion was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_opinion
@opinion = Opinion.find(params[:id])
end
# Only allow a list of trusted parameters through.
def opinion_params
params.require(:opinion).permit(:user, :body, :user_id)
end
end
用户表单
<%= form_with(model: @new_opinion, local: false) do |form| %>
<div class="field rich p-3">
<div class="control">
<%= form.rich_text_area :body, label: "What's Your Opinion", input_html: {class: "textarea"}, wrapper: false, label_html: {class: "label"}, placeholders: "Your opinion...", autofocus: true %>
</div>
</div>
<%= form.button :submit %>
<% end %>
意见表
<%= form_with(model: @opinion, local: true) do |form| %>
<div class="field rich p-3">
<div class="control">
<%= form.rich_text_area :body, label: "What's Your Opinion", input_html: {class: "textarea"}, wrapper: false, label_html: {class: "label"}, placeholders: "Your opinion...", autofocus: true %>
</div>
</div>
<%= form.button :submit, class: "button is-info" %>
<% end %>
如果你们能帮我解决这个问题,我将不胜感激,这是我的最后一个项目
【问题讨论】:
-
问题是用户表单指向一个名为@new_opinion的对象,假设这是一个Opinion实例。这意味着它将路由到 OpinionsController#create。
-
我知道 :( 我不知道解决这个问题的正确方法是什么
-
好的,所以我仔细阅读了,发现用户秀上有一个意见表。这里有两点: 1:我建议对两种形式都使用一个部分。 2:您正在寻找的答案可能是使用 hidden_field_tag 传递一个变量,然后再传递给重定向。在表单中它是
-
@AJFaraday 非常感谢,效果很好
-
OP 或@AJFaraday 可以在这里发布答案,这样它就不会出现未解决的问题吗?
标签: ruby-on-rails ruby ruby-on-rails-5