0
我正在使用Rails應用程序。我有一個新帖子的表格,包括標題,圖片,網址鏈接和描述。但是,我遇到了一個問題,那就是每當用戶嘗試更新(編輯)該帖子時,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
我找不到類似我的問題的東西,請大家幫幫忙!謝謝
的URL鏈接值的佔位符消失,或者他們是重定向?瀏覽器中有什麼?你能爲此發佈控制器嗎? – gwalshington
我很抱歉,您是指重定向的意思? URL鏈接是表單的一部分,但每當用戶嘗試更新已存在的帖子時,該帖子中都有一個URL鏈接。更新後,URL鏈接變爲空白。我在Chrome中執行此操作。我也加了我的帖子控制器,謝謝.. – hekmat