I have a form partial current setup like this to make new blog posts
<% form_for([#current_user, #post]) do |f| %>
This works great when editing a post, but when creating a new post I get the following error:
undefined method `user_posts_path' for #<ActionView::Base:0x6158104>
My routes are setup as follows:
map.resources :user do |user|
user.resources :post
end
Is there a better way to setup my partial to handle both new posts and editing current posts?
map.resources :user do |user|
user.resources :posts
end
pluralize your model names in routes declaration. as you can see it says resourc-es, so you must use user-s and post-s too.
Controllers should be named UsersController and PostsController
Models should be named User and Post.
if above example still does not work for you, try a this one
map.resources :users do |u|
u.resources :posts
end
Related
I have model based form:
<h2>Add New Credit Card</h2>
<%= form_for #credit_card do |f| %>
some fields
<% end %>
routes:
resources :credit_card
credit_card_index GET /credit_card(.:format) credit_card#index
POST /credit_card(.:format) credit_card#create
new_credit_card GET /credit_card/new(.:format) credit_card#new
edit_credit_card GET /credit_card/:id/edit(.:format) credit_card#edit
credit_card GET /credit_card/:id(.:format) credit_card#show
PATCH /credit_card/:id(.:format) credit_card#update
PUT /credit_card/:id(.:format) credit_card#update
DELETE /credit_card/:id(.:format) credit_card#destroy
controller:
def new
#credit_card = CreditCard.new
end
When I try to render by form it says:
undefined method `credit_cards_path' for #<#<Class:0x00000004c37680>:0x00000004c34570>
Did you mean? credit_card_path
credit_card_index_path
credit_card_url
Its a model based form, for now I have nothing in model. I just want to render and submit will go to create method
You're using the Singular Resources:
resources :credit_card
Where you have to use Plural Resources:
resources :credit_cards
In your routes, use plural for resources definition.
resources :credit_cards
That will generate your routes like
credit_cards GET /credit_cards/:id(.:format) credit_card#show
Use resources :credit_cards instead of resources :credit_card
I have 3 models: posts, comments and questions. In routes.rb they look like this.
resources :posts do
resources :comments do
end
end
resources :comments do
resources :questions do
end
end
I am trying to link to the comment_questions path from a _post partial (being called in the posts index view) with the following:
<%= link_to (comment.body), comment_questions_path(post, comment) %>
It links to the comment_questions path but goes to a question belonging to the wrong comment.
Thanks!
You should be passing in the comment and question object to comment_question_path(comment, question) if you are linking to the show action of QuestionsController
The url generated for comment_question_path will be
GET comments/:comment_id/questions/:id questions#show
With what you are doing, comment_questions_path(post, comment), it will use post id for comment and comment id for question and you will go to a totally different page
I have a questions controller and an associated model and a number of rest routes. Here is how it's set up in routes.rb:
resources :questions
I want to add a custom route that has the format /questions/widget/ID (where ID is the id of the question for which I want to generate a widget). I want this to be processed by the "widget" action in my questions controller. I've tried a number of things such as:
resources :questions do
member do
get 'widget/:id'
end
end
But nothing is working. I'm sure I'm missing something simple. Any ideas? Thanks in advance.
You do not have to specify the id since you are inside resources. It should look like:
resources :questions do
member do
get 'widget'
end
end
You can get more information from the Rails Guide. Look at section 2.9.1.
Edit: I just noticed that you are trying to match get /questions/widget/:id. This will set up a route for get /questions/:id/widget. This is more in line with Rails convention. If you really want it the other way, you need to set up a custom match statement:
match "/questions/widget/:id" => "questions#widget"
However, I would stick with convention.
I know it is old, but looking to fix another routing problem I ended here, it is possible, to do what you are asking for, here is an example
resources :articles do
get 'by_tag/:tag' => :by_tag, on: :collection
get 'by_author/:author' => :by_author, on: :collection
resources :comments, except: :show
end
now you have /artices/by_tag/:tag . The trick was to use on:collection.
Obviously don't forget to add the by_tag action and by_author.
class ArticlesController < ApplicationController
.....
def by_tag
...
end
end
Check this route works with
melardev#local~$ rails routes
Why don't you use this routes:
resources :questions do
resources :widgets
end
it will create path like questions/:question_id/widgets/new for you to create new widget for question with specific id of question.
This is what ended up working for me:
resources :post do
get "author/:author", to: "posts#author", on: :collection, as: "author"
end
Which outputs the following route:
author_posts GET /posts/author/:author(.:format) posts#author
Then in your controller, you need to create the author action:
class PostsController < ApplicationController
def author
#roles = Post.where(author: params[:author])
render :index # to reuse the index view
end
end
Then in your view:
<%= link_to post.author, author_posts_path(post.author), data: { turbo_frame: "_top" } %>
Rails newbie here (trying to get these questions answered)
I'm using Ryan Bates' Rails Cast on Wicked Wizard Forms to create a multi-step form. I'm getting a "No route matches [POST] "/user_steps/gender" (where gender is a view under the user_steps controller).
Any ideas?
routes.rb:
Store::Application.routes.draw do
resources :likes
resources :categories
resources :users
resources :user_steps
root to: 'static_pages#home'
user_steps controller:
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :gender, :items, :brands, :final
def show
render_wizard
end
def update
#user.attributes = params[:user]
render_wizard
end
end
When you define a new method, rails will default to a get request on that method. In order to make it a post method, try adding
match "user_steps/gender", to: "user_steps#gender", via: "post"
Check out this routes reference
I'm looking for rails wizards solutions and was going through "#346 Wizard Forms with Wicked" railscas when I ran into the same issue.
Adding a route, as #Klipfel suggests, is a good "rails" answer but not the correct approach for the wicked gem. If you add a route pointing to another controller method you are routing the request outside of the wicked framework.
I resolved this problem by specifying put for the http method. In the #346 Wizard Forms with Wicked railscast case it would look like this:
<%= form_for #user, url: wizard_path, method: :put do |f| %>
I'm not sure why it works in the railscast
I created a new file in app/views/students called courses.html.erb
Then I try to reference it at app/views/students/show.html.erb:
<%= link_to 'courses', courses_student_path(#student) %>
However I am getting
undefined method `courses_student_path' for #<#:0x1052d1648>
What step did I miss?
Note that you never link to views. It is always some action in some controller which in turn renders that view. In this case your action is courses in students controller and you need to create a route for it.
Assuming you already had :students resource defined in config/routes.rb:
resources :students do
get 'courses', :on => :member
end
This will give you urls like students/1/courses and route helpers courses_student_path and courses_student_url.
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions