编辑:感谢您的回复,我尝试了下面列出的所有内容,但现在仍然运气好。如果我键入localhosts / posts / new,它会将我带到表单,但是当我从导航栏中单击时,链接不起作用。我已经更新了代码并包含了我的佣金路线结果。
我是ruby的新手并且正在完成一个教程,但是我的一个链接无效,不确定发生了什么。
我创建新帖子的导航链接没有带我到正确的页面,当我点击链接到“posts_path”页面没有改变。
我可以通过在地址栏中键入/ posts / new来创建新帖子,但是当我点击导航栏上的“新帖子”链接时页面不会更新(尽管网址显示/帖子)。知道如何解决这个问题吗?
配置/ routes.rb中
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get '/search', to: 'static_pages#search'
get '/login', to: 'sessions#new'
get '/posts', to: 'posts#new', as: 'new_post'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/signup', to: 'users#new'
get 'users/new'
get 'static_pages/home'
get 'posts/new'
get 'sessions/new'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :posts, only: [:new, :create, :destroy]
end
应用程序/视图/布局/ _header.html.erb
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<% link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Search", search_path %></li>
<% if logged_in? %>
<li><%= link_to "Users", users_path %></li>
<li><%= link_to "Posts", posts_new_path %></li>
<li class="dropdown">
<a href='#' class="dropdown-toggle" data-toggle="dropdown">Account <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li><%= link_to "Logout", logout_path, method: "delete" %></li>
</ul>
</li>
<% else %>
<li><%= link_to "Log in", login_path %></li>
<% end %>
</ul>
</nav>
</div>
posts_controller.rb
class PostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = "Post created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
@post.destroy
flash[:success] = "Post Deleted"
redirect_to request.referrer || root_url
end
def new
@post = current_user.posts.build if logged_in?
end
private
def post_params
params.require(:post).permit(:description, :picture)
end
def correct_user
@post = current_user.posts.find_by(id:params[:id])
redirect_to root_url if @post.nil?
end
end
rake routes:
password_resets_new GET /password_resets/new(.:format) password_resets#new
password_resets_edit GET /password_resets/edit(.:format) password_resets#edit
sessions_new GET /sessions/new(.:format) sessions#new
root GET / static_pages#home
search GET /search(.:format) static_pages#search
login GET /login(.:format) sessions#new
new_post GET /posts(.:format) posts#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
signup GET /signup(.:format) users#new
users_new GET /users/new(.:format) users#new
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_about GET /static_pages/about(.:format) static_pages#about
static_pages_search GET /static_pages/search(.:format) static_pages#search
posts_new GET /posts/new(.:format) posts#new
GET /password_resets/new(.:format) password_resets#new
GET /password_resets/edit(.:format) password_resets#edit
GET /sessions/new(.:format) sessions#new
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
edit_account_activation GET /account_activations/:id/edit(.:format) account_activations#edit
password_resets POST /password_resets(.:format) password_resets#create
new_password_reset GET /password_resets/new(.:format) password_resets#new
edit_password_reset GET /password_resets/:id/edit(.:format) password_resets#edit
password_reset PATCH /password_resets/:id(.:format) password_resets#update
PUT /password_resets/:id(.:format) password_resets#update
posts POST /posts(.:format) posts#create
GET /posts/new(.:format) posts#new
post DELETE /posts/:id(.:format) posts#destroy
答案 0 :(得分:3)
删除get 'sessions/new'
&amp; get '/posts', to: 'posts#new', as: 'new_post'
重启服务器
并尝试link_to "Posts", new_post_path, method: :post
应该工作
答案 1 :(得分:2)
我建议您删除get 'posts/new'
&amp; get '/posts', to: 'posts#new', as: 'new_post'
因为这些是重复的路线。
您已在路线resources :posts, only: [:new, :create, :destroy]
中创建了正确的路线。通过包含new
,Rails正在为您做这一部分。见这里:http://guides.rubyonrails.org/v4.2/routing.html#crud-verbs-and-actions
然后您应该能够使用其他人发布的链接。
<%= link_to "New Post", new_post_path %>
基于此处的指南:http://guides.rubyonrails.org/v4.2/routing.html#path-and-url-helpers
答案 2 :(得分:2)
问题是您的路线发生冲突。
您已为func textViewDidChange(_ textView: UITextView) {
let pos = textView.endOfDocument
let currentRect = textView.caretRect(for: pos)
if previousRect != CGRect.zero {
if currentRect.origin.y > previousRect.origin.y {
// Array of Strings
var currentLines = textView.text.components(separatedBy: "\n")
// Remove Blank Strings
currentLines = currentLines.filter{ $0 != "" }
//increase the counter counting how many items inside the array
counter = currentLines.count
}
}
previousRect = currentRect
}
new
和
get /posts', to: 'posts#new', as: 'new_post'
现在resources :posts, only: [:new, :create, :destroy]
有两条路径。我建议你删除第一个。将 html 更改为
posts#new
它应该可以正常工作。
答案 3 :(得分:2)
我查看了你的代码...这就是我建议添加/编辑更改...并确定它应该有效:)。
routes.rb
,看起来不错BUT
有一个重复的网址条目。要么删除get 'posts/new'
,要么resources :posts, only: [:new, :create, :destroy]
,因为它们都会为posts#new
创建重复条目。您可以通过输出rake routes >> path.txt
app/views/layouts/_header.html.erb
中,它也很好,不需要任何改变。posts_controller.rb
中,仅为登录用户添加了条件,请检查您是否已登录,否则请删除该条件并再次检查。F12
进行调试,看看你在之前的网页浏览器控制台中获得了什么日志,之后点击查看是否有一些日志是不是javascript错误。答案 4 :(得分:1)
链接到新帖子的代码
<%= link_to "New Post", new_post_path %>
答案 5 :(得分:1)
尝试new_post_path
此外,终端中的rake routes
将列出所有路由和相关帮助,供您进行双重检查。
最后,添加&#39;作为&#39; to:get '/posts', to: 'posts#new', as: 'new_post'
在您的路线文件中将允许您定义自己的路径名称(在本例中为new_post_path,但您可以将其设置为您喜欢的任何路径)。
答案 6 :(得分:1)
<%= link_to "Posts", posts_path %> corresponds to index action not to the new action.
删除routes.rb中的posts控制器的唯一条件,它将创建所需的所有基本路由而不是特定路由。
resources :posts
因此,new_post_path返回/ posts / new
最后添加链接链接<%= link_to 'New Post', new_post_path %>
答案 7 :(得分:1)
这可能有助于重定向,
在 routes.rb ,
:
它提供了所有默认控制器方法的路径,如index,new,create,edit,update,show,destroy。然后检查耙路线。获取相应的路径,如
resources :posts
如果正确则意味着给出网址,
new_post GET /posts/new(.:format) posts#new
它重定向到控制器中的new方法并呈现相应的html.erb页面。这个网址由routes.rb。
或强>
手动推送重定向网址。在html页面中,
<%= link_to 'New Post', new_post_path %>
在routes.rb中,
<%= button_tag "New", :onclick => "getNew()" %>
function getNew(){
var form = document.forms[0];
// get the id and value with any variable //
form.method = "post";
// pass the value to parameters //
form.var name = column name; (description r picture)
form.action = "posts/new";
form.submit();
}
它使手动重定向URL。它调用了新的控制器方法 让我知道您的反馈!感谢
答案 8 :(得分:1)
Rails从上到下扫描路径即。的routes.rb 即可。每当它找到匹配的路径时, controller#action 就会被执行,并且将跳过剩余的所有路径(找到匹配后的后续路线)。
在这种情况下,路线是冲突的,所以请改变一切,一切都会好起来的。 配置/ routes.rb中
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get '/search', to: 'static_pages#search'
get '/login', to: 'sessions#new'
#get '/posts', to: 'posts#new', as: 'new_post' #not required as resources :posts will do the job in last line
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/signup', to: 'users#new'
get 'users/new'
get 'static_pages/home'
get 'posts/new', to: 'posts#new', as: 'posts_new_path' #Made changes here according to helper used in view 'posts_new_path'
get 'sessions/new'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :posts, only: [:new, :create, :destroy]
end
答案 9 :(得分:1)
根据您的佣金路线输出,以下内容应该有效
<%= link_to "New Post", posts_new_path %>
请点击新帖子链接,告诉我们控制台日志中是否有任何错误。
答案 10 :(得分:1)
底线是您尝试以非Rails方式执行操作。 Rails倾向于约定优于配置,这意味着如果你不以Rails的方式做事,你将会遇到糟糕的时间。
这意味着您应该清理路线,直到没有不必要的重复。现在,您有3条不同的路由到posts#new
,每条路径都有不同的路径前缀。
我会删除:
get '/posts', to: 'posts#new', as: 'new_post'
get 'posts/new'
因为resources :posts, only: [:new, :create, :destroy]
已经创建了该路由。然后你的new_post_path
助手应该创建正确的URL,你的路线不会有歧义。