【问题标题】:I have the same form in two pages and i need to redirect to the same page of the form我在两个页面中有相同的表单,我需要重定向到表单的同一页面
【发布时间】: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


【解决方案1】:

解决方案是添加一个 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

【讨论】:

    【解决方案2】:

    Rails 5 有 redirect_back

    &然后改变

    redirect_to opinions_url
    

    redirect_back(fallback_location: root_path)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-11
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      • 2023-03-10
      • 1970-01-01
      • 2014-03-23
      相关资源
      最近更新 更多