我的脚手架模型与create
动作效果不佳。
我发现如果按下提交按钮,它会链接到TalksController#index
,但应该链接到TalksController#create
我不知道如何修复链接以创建操作。 _form.html.erb
和new.html.erb
Create
2天前运作良好。
[edit]我发布了我的代码。 我添加了一些除了脚手架代码之外的代码
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 %>
[SECOND EDIT] 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
答案 0 :(得分:0)
索引和create都引用相同的url或相同的路由方法,唯一的区别是方法如果你将方法用作post,那么它将创建否则它将转向索引。但是,如果传入要创建的对象的新实例(例如@customer = Customer.new),则可以隐式处理此问题,然后将@customer传递给form_for方法。
答案 1 :(得分:0)
使用form_for方法,您可以指定要执行的操作的url,或者仅指定控制器和操作:
<%= form_for @post, :url => {:controller => "your-controller-name", :action => "your-action-name"} do |f| %>
在你的行动(“你的行动名称”)中,你可以调用你的参数,如:
post_id = params[:post][:id]
希望能帮助你。
答案 2 :(得分:0)
调整你的路线.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
置于默认/ catchall路由之上。路线在第一场比赛时处理,所以如果你需要那些默认/ catchalls,你可以把它们放在底部。话虽这么说,除非你对它们有要求,否则大多数现代的rails应用程序默认不需要它们,所以你可以注释掉/删除它们作为另一种选择。