rails用锚点渲染动作

use*_*183 6 anchor render simple-form ruby-on-rails-4

我有一个带有simple_form gem的表单.如果我使用锚标记对浏览器中的网址进行数字处理,则可以:

localhost:3000/contacts/#new_contact #show me the correct form in the page
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

class ContactsController < ApplicationController

  def index
    @message = Contact.new()
  end

  def create

    @message = Contact.new(msg_params)
    @message.request = request
    if @message.deliver
      redirect_to contacts_path , flash: { success: 'Ok' }
    else
      render action: :index #how can i render the new_contact anchor??
    end

  rescue   ScriptError
    redirect_to contacts_path , flash: { error: 'spam' }
  end

  private
  def msg_params
    params.require(:contact).permit(:name,:email,:message,:spam)
  end
end
Run Code Online (Sandbox Code Playgroud)

这是我的表格:

<%= simple_form_for @message, defaults: { label: false} do |f| %>
    <%= f.input :name %>
    <%= f.input :email %>
    <%= f.input :message, :as => :text,input_html: { rows: 7 } %>

    <div class="hidden">
      <%= f.input :spam, :hint => 'Leave this field blank!' %>
    </div>

    <%= f.button :submit, 'Send', class: 'btn-block btn-success btn-lg' %>

<% end %>
Run Code Online (Sandbox Code Playgroud)

如何使用render方法显示new_contact锚点?

mal*_*tor 2

您不能用来render修改锚标记。事实上,render与 URI 完全无关。

您可以使用现有链接链接到“/url#anchor”,重定向到那里,或使用 javascript 来达到相同的效果。

:id => 'anchor'如果表单上未设置锚点本身,您可能还需要添加。