我正在开发一个rails应用程序。我有一个新帖子的表单,其中包括标题,图片,URL链接和说明。但是,我遇到了一个问题,即每当用户尝试更新(编辑)帖子时,URL链接就会消失。用户每次必须更新帖子时都必须重写URL链接!
这是我的edit.html.haml:
.col-md-6.col-md-offset-3
.row
.panel.panel-default
.panel-heading
%h1 Edit Project
.panel-body
= simple_form_for @post, html: { multipart: true } do |f|
- if @post.errors.any?
#errors
%h2
= pluralize(@post.errors.count, "error")
prevented this Post from saving
%ul
- @post.errors.full_messages.each do |msg|
%li= msg
.form-group
= f.input :title,:label => "Project Name", input_html: { class: 'form-control', :type => "text", :required => ""}
.form-group
= f.input :image,:label => "Image", input_html: { class: 'form-group',"aria-describedby" => "fileHelp", :required => "", :type => "file" }
.form-group
= f.input :link,:label => "Project Link", input_html: { class: 'form-control', :type => "url", :value => "https://", :required => "" }
.form-group
= f.input :description,:label => "Description", input_html: { class: 'form-control', :rows => "3", :required => "" }
%button.btn.btn-info{:type => "submit" } Update Project
这是我的posts_controller.rb
:
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy, :upvote]
before_action :authenticate_user!, except: [:index, :show, :most_liked]
def index
@posts = Post.all.order("created_at DESC")
end
def most_liked
@most_liked = Post.all.order(cached_votes_score: :DESC)
end
def show
@comments = Comment.where(post_id: @post)
@random_post = Post.where.not(id: @post).order("RANDOM()").first
end
def new
@post = current_user.posts.build
end
def create
@post = current_user.posts.build(post_params)
respond_to do |format|
if @post.save
flash[:success] = 'YEs'
format.html { redirect_to @post }
format.json { render :show, status: :created, location: @post }
else
flash[:danger] = 'no'
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def edit
end
def update
respond_to do |format|
if @post.update(post_params)
flash[:success] = 'Good'
format.html { redirect_to @post }
format.json { render :show, status: :ok, location: @post }
else
flash[:danger] = 'bad'
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def destroy
@post.destroy
redirect_to root_path
end
def upvote
@post.upvote_by current_user
redirect_to :back
end
private
def find_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :link, :description, :image)
end
end
我找不到与我的问题类似的东西,请帮忙!感谢
答案 0 :(得分:0)
通过使用:value,您将覆盖将放置在那里的字段的值。您可以使用:占位符代替。
更改此行
= f.input :link,:label => "Project Link", input_html: { class: 'form-control', :type => "url", :value => "https://", :required => "" }
使用占位符而不是像这样的值
= f.input :link,:label => "Project Link", input_html: { class: 'form-control', :type => "url", :placeholder => "https://", :required => "" }