Ruby 关于 Rails/How 脚手架形式 link 具体操作?

Ruby on Rails/How to Scaffold form link to specific action?

我的脚手架模型不能很好地与 create 动作配合使用。 我发现如果我按下提交按钮,它会 linked 到 TalksController#index 但它应该 linked 到 TalksController#create

我不知道如何将其修复为 link 以创建动作。 _form.html.erb 和 new.html.erb

中没有相关代码

Create 2 天前表现良好。

[edit]我 post 我的代码。 除了脚手架自己制作的代码外,我还添加了一些代码

talks_controller.rb

  class TalksController < ApplicationController
  before_action :set_talk, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [ :index, :show, :all ]

  # GET /talks
  # GET /talks.json

  def all
    @talks = Talk.all
    @talks = Talk.order(created_at: :desc)
  end

  def index
    @talks = Talk.all
    @talks = Talk.order(created_at: :desc)

    @whichboard = params[:whichboard]
    @seq = params[:saveit]
    @title = params[:savetitle]
  end

  # GET /talks/1
  # GET /talks/1.json
  def show
  end

  # GET /talks/new
  def new
    @talk = Talk.new
    @seq = params[:seq]
    @whichboard = params[:whichboard]
  end

  # GET /talks/1/edit
  def edit
    authorize_action_for @talk
  end

  # POST /talks
  # POST /talks.json
  def create
    @talk = Talk.new(talk_params)
    @talk.user_id = current_user
    @talk.seq = params[:talk][:seq]
    @talk.where = params[:talk][:where]

    respond_to do |format|
      if @talk.save
        format.html { redirect_to @talk, notice: 'Talk was successfully created.' }
        format.json { render :index, status: :created, location: @talk }
      else
        format.html { render :new }
        format.json { render json: @talk.errors, status: :unprocessable_entity }
      end
    end

  end

  # PATCH/PUT /talks/1
  # PATCH/PUT /talks/1.json
  def update
    authorize_action_for @talk
    respond_to do |format|
      if @talk.update(talk_params)
        format.html { redirect_to @talk, notice: 'Talk was successfully updated.' }
        format.json { render :show, status: :ok, location: @talk }
      else
        format.html { render :edit }
        format.json { render json: @talk.errors, status: :unprocessable_entity }
      end
    end

  end


  # DELETE /talks/1
  # DELETE /talks/1.json
  def destroy
    authorize_action_for @talk
    @talk.destroy
    respond_to do |format|
      format.html { redirect_to talks_url, notice: 'Talk was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_talk
      @talk = Talk.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def talk_params
      params.require(:talk).permit(:content, :user_id, :seq, :where)
    end
end

_form.html.erb

<%= simple_form_for(@talk) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :content %>
    <%#= f.association :user %>
    <%= f.input :seq,  :as => :hidden, :input_html => { :value => @seq } %>
    <%= f.input :where,  :as => :hidden, :input_html => { :value => @whichboard } %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

new.html.erb

<div class="row">
  <div class='col-md-12'>
    <p style='text-align:right;'>
      created by <%= current_user.name %>, Current Time : <%= Time.now %>, board <%= @whichboard %> seq <%= @seq %>
    </p>
  </div>
</div>
<%= render 'form' %>
<%= link_to 'Back', talks_path %>

[第二次编辑]routes.rb

Rails.application.routes.draw do

  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

  root 'cpu#index'
  post ':controller(/:action(/:id(.:format)))'
  get ':controller(/:action(/:id(.:format)))'


  resources :talks
end

index和create都指的是同一个url或者同一个route方法,唯一不同的是方法如果你用的方法是post那么会去create否则它会转到索引。但是,如果您传入要创建的对象的新实例,例如@customer = Customer.new,则可以隐式处理此问题,然后将@customer 传递给form_for 方法。

使用 form_for 方法,您可以指定 url 您想要执行的操作,或者仅指定控制器和操作,如下所示:

<%= form_for @post, :url => {:controller => "your-controller-name", :action => "your-action-name"} do |f| %>

在您的操作 ("your-action-name") 中,您可以像这样调用参数:

post_id = params[:post][:id]

希望对你有所帮助。

调整你的routes.rb

Rails.application.routes.draw do

  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

  root 'cpu#index'


  resources :talks

  #do you really need these? 
  post ':controller(/:action(/:id(.:format)))'
  get ':controller(/:action(/:id(.:format)))'



end

resources :talks 置于 default/catchall 路线之上。路由在第一次匹配时处理,所以如果你需要那些 default/catchalls 你可以把它们放在底部。话虽如此,除非您对它们有要求,否则大多数现代 rails 应用默认不需要它们,因此您可以将它们作为另一种选择注释掉/删除。