私はレールアプリを使っています。タイトル、画像、URLリンク、説明を含む新しい投稿用のフォームがあります。しかし、私は問題に直面し、ユーザーが投稿を更新(編集)しようとするたびに、URLリンクが消えます。ユーザーは投稿を更新するたびにURLリンクを書き直さなければなりません!ここに私のposts_controller.rb
フォームで投稿を編集した後にURLを保存する方法
.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
:
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
私は私の質問に似た何かを見つけることができませんでした、助けてください
は、ここに私のedit.html.hamlです!ありがとう
URLリンクなどの値のプレースホルダを使用するには、このラインが消え、またはそれらをリダイレクトしていますか?ブラウザには何がありますか?あなたはこれのコントローラを投稿できますか? – gwalshington
申し訳ありませんが、リダイレクトはどういう意味ですか? URLリンクはフォームの一部ですが、ユーザーがURLリンクを持つ既存の投稿を更新しようとするたびに発生します。更新後、URLリンクは空白になります。私はChromeでこれをやっています。私はまた、私の投稿コントローラを追加しました、ありがとう.. – hekmat