我有两页相同的表格,我需要重定向到表格的同一页



我的问题

我在两个不同的页面上为一个模型(意见和用户(有两个表单。当我在用户页面上点击提交表单时,它会将我重定向到意见页面,我知道我可以重定向到我在操作创建中重定向的同一页面,但有没有方法将用户表单重定向到用户页面

用户控制器

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 %>

如果你们能帮我,我会非常感激的,这是我的最后一个项目

解决方案是添加一个hiddin_field_tag并将每个表单重定向到所需的路径,如下所示:

<%= form_with(model: @new_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>
<%= hidden_field_tag(:redirect_url, user_path(@user.id)) %>
<%= form.button :submit   %>
<% end %>

并将控制器中的创建操作重定向到参数


def create
@opinion = @user.opinions.build(opinion_params)
respond_to do |format|
if @opinion.save
format.html { redirect_to(params[:redirect_url]) }
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

Rails 5具有redirect_back

&然后更改

redirect_to opinions_url

redirect_back(fallback_location: root_path)

相关内容

  • 没有找到相关文章

最新更新