I am trying to create the form on my "new" page. I am using form_with. I have already checked my routes, controllers and views, but I have not yet identified the problem.
The error that is returning is:
NameError in Pages # new
undefined local variable or method `post 'for # <# <Class: 0x00007f5640e1f440>: 0x00007f5640e1c060>
Did you mean? #post
Extracted source (around line # 5):
<% = form_with (model: post, location: true) do | form | %>
<div class = "form-group">
<% = form.label: title%>
Below is my form using form_with:
<h1> CREATE NEW ARTICLE </h1>
<% = form_with (model: post, location: true) do | form | %>
<div class = "form-group">
<% = form.label: title%>
<% = form.text_field: title, class: 'form-control'%>
</div>
<div class = "form-group">
<% = form.label: author%>
<% = form.text_field: author, class: 'form-control'%>
</div>
<div class = "form-group">
<% = form.label: body%>
<% = form.text_area: body, class: 'form-control', rows: 10%>
</div>
<% = form.submit class: 'btn btn-primary', data: {disable_with: 'Creating ..'}%>
<% end%>
this is my controller:
class PagesController <ApplicationController
def articles
#posts = Post.all
end
def new
#post = Post.new
end
def show
#post = Post.find (params [: id])
end
def create
#post = # Post.new (post_params)
# post.save
redirect_to article_path (#post)
end
private
def post_params
params.require (: post) .permit (: title,: author,: body)
end
end
Now here are my routes:
Rails.application.routes.draw do
root 'pages # index'
get 'articles', to: 'pages # articles'
get 'articles / new', to: 'pages # new'
get 'articles /: id', to: 'pages # show', as: 'article'
post 'articles', to: 'pages # create'
end
Replace
<% = form_with(model: post, location: true) do | form | %>
.....
<% end %>
with
<% = form_with(model: post, local: true) do | form | %>
....
<% end %>
and new.html.erb should contains
<%= render 'form', post: #post` %>
And update your controller with posts_controller.rb
class PostsController < ApplicationController
before_action :find_post, only: [:show]
def index
#posts = Post.all
end
def new
#post = Post.new
end
def show
end
def create
#post = Post.new(post_params)
#post.save
redirect_to #post
end
private
def find_post
#post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :author, :body)
end
end
Routes should be as
get 'posts', to: 'posts#index'
get 'posts/new', to: 'posts#new'
get 'posts/:id', to: 'posts#show'
post 'posts', to: 'posts#create'
or simply as
resources :posts, only: [:index, :new, :show, :create]
There's quite a lot of whitespace issues going on in your code snippets, but I think that might be a side effect of however you've copied the code rather than any deliberate cause.
The error you're seeing is because the form_with method doesn't know what post is. In your controller, you set up #post in your new method, which means that new.html.erb will be able to see the instance variable #post, but it won't know what post (without the #) is.
So if the form is in new.html.erb, you'd need to use #post in place of post here.
That said, common Rails behaviour is to have the form in its own partial _form.html.erb. That might be the case here (you don't specify the file names of your views). In that case, you can tell the partial to use local variables rather than the instance variable that the main template file uses.
To do that, you need to map an instance variable to a local one in the render call to your form, for example:
# in new.html.erb
<%= render 'form', post: #post` %>
This is saying 'take the object that I can see as #post, and make it available as the post local variable within the form template'.
Related
im making a twitter clone and trying to make it so the users username appears next to their tweet.
Ive made it work through adding a user and a tweet in the seed file, hoever when i add a create,new method and a form it comes up with the error "Couldn't find User without an ID" and highlighting the first line of my create method. not sure what the issue is, thanks.
class TweetsController < ApplicationController
before_action :authenticate_user!, :except => [:index, :new, :create]
def index
#tweets = Tweet.all.order("created_at DESC")
#tweet = Tweet.new
end
def show
#tweet = Tweet.find(params[:id])
end
def new
# #tweet = Tweet.new
end
def create
#user = User.find(params[:id])
#tweet = Tweet.new(tweet_params)
#tweet.user = #user
if #tweet.save
redirect_to tweets_path
end
end
def edit
#tweet = Tweet.find(params[:id])
end
def update
#tweet = Tweet.find(params[:id])
#tweet.update(tweet_params)
redirect_to tweets_path
end
private
def tweet_params
params.require(:tweet).permit(:user_id,:content)
end
end
<h1>TWEETS</h1>
<%= simple_form_for [#user,#tweet], id: "form-submit" do |f| %>
<%= f.input :content, label: "Tweet" %>
<%= f.input :user %>
<%= f.button :submit, class: "btn btn-danger" %>
<% end %>
<br>
<% #tweets.each do |tweet| %>
<ul>
<li>
<%= tweet.created_at.strftime("%B %d %Y, %l:%M%P") %> <br>
<%= tweet.content %>
<%= tweet.user.username %>
</li>
</ul>
<% end %>
You need to define #user in a variable in your index method.
Any variable you use in the form needs to be declared somewhere, either in the helper, controller, or view. Rails convention is to declare them in the controller normally.
I would need to see your config/routes.rb file for the error message you are getting in the image, but if you type rails routes at the command line, you can see a list of all available routes, when you use:
simple_form_for [#user, #tweet]
Rails will interpret [#user, #tweet] as user_tweets_path, and try to submit the form to this path. That path is defined in your config/routes.rb file.
The error is telling you that you have not defined this path in the routes file. To define this path you can add this line to your routes file:
resources :users do
resources :tweets
end
I'm running into a NoMethodError in my Users#show when trying to include a form partial for submitting a task item (_form.html.erb). My other partial (_item.html.erb) is rendering properly. My item model and my user model are related to each other, user has_many :items, and item belongs_to :user.
Any and all help would be greatly appreciated!
Below is my routes.rb
Rails.application.routes.draw do
get 'welcome/index'
get 'welcome/about'
root 'users#show'
resources :users do
resources :items, only: [:new, :create]
end
devise_for :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Below is my terminal output
ActionView::Template::Error (undefined method `items_path' for #<#<Class:0x007fefeca61dd0>:0x007fefeca58b18>
Did you mean? items_create_path):
1: <h4> Create a new to-do item </h4>
2:
3: <%= form_for #item do |f| %>
4: <%= f.label :name %>
5: <%= f.text_field :name, class: 'form-control', placeholder: "Enter task here" %>
Items Controller
class ItemsController < ApplicationController
def new
#item = Item.new
end
def create
#item = Item.new
#item.user = current_user
#item.name = params[:item][:name]
if #item.save
flash[:notice] = "Item saved"
redirect_to #item
else
flash.now[:alert] = "Item was not created, please try again."
render :new
end
end
end
Users Controller
class UsersController < ApplicationController
def show
if !current_user.nil?
#user = current_user
#item = Item.new
#items = #user.items
else
redirect_to new_user_registration_path
end
end
end
Users#show page
<h2> Your to-do list</h2>
<div class="col-md-8">
<div class='items'>
<%= render partial: "items/item", local: { item: #item} %>
</div>
</div>
<div class="col-md-8">
<div class='new-item'>
<%= render partial: "items/form", local: { item: #item } %>
</div>
</div>
In your routes.rb file, you have items defined as a nested resource. You can check all of your routes by running this command on your terminal: rake routes.
For your routes specifically, you say:
resources :users do
resources :items, only: [:new, :create]
end
This would give you routes for GET /users/:user_id/items/new and POST /users/:user_id/items. However, in your form, it looks like you're trying to do this: <%= form_for #item do |f| %>. You don't have a route defined for an item by itself. You'll need to supply a user as well.
Try this for your form:
<%= form_for [#user, #item] do |f| %>
And something like this in your ItemsController:
class ItemsController
def new
#user = current_user
#item = Item.new
end
end
Your items route is nested under users. therefore you have to pass both to the form_for - a user and an item.
Something like this will probably work:
<%= form_for(current_user, #item) do |f| %>
So I've been building an app using rails and the google datastore. I keep encountering an error in my new.html.erb file where I get the NoMethodError.
I've scoured looking for a solution to my problem and it's most likely a simple typo somewhere in my code. I've been staring at the code for hours so maybe some fresh new eyes can help me figure it out
This is my posts_controller.rb
class PostsController < ApplicationController
PER_PAGE = 10
def index
#post, #cursor = Post.query limit: PER_PAGE, cursor: params[:cursor]
end
def new
#post = Post.new
end
def create
#post = Post.new post_params
if #post.save
flash[:success] = "Posted"
redirect_to posts_path(#post)
else
render :new
end
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update(post_params)
redirect_to #post
else
render 'edit'
end
end
def show
#post = Post.find(params[:id])
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
This is my new.html.erb
<h1>Create Post</h1>
<%= form_for #post do |f| %>
<% if #post.errors.any? %>
<% #post.errors.full_messages.each do |msg| %>
<div class="alert alert danger"><%= msg %></div>
<% end %>
<% end %>
<div class="form-group">
<%= f.label:title %><br>
<%= f.text_field(:title, {:class => 'form-control'}) %>
</div>
<div class="form-group">
<%= f.label:body %><br>
<%= f.text_area(:body, {:class => 'form-control'}) %>
</div>
<p>
<%= f.submit({:class => 'btn btn-primary'}) %>
</p>
<% end %>
This is my model post.rb
class Post
attr_accessor :title, :body
# Return a Google::Cloud::Datastore::Dataset for the configured dataset.
# The dataset is used to create, read, update, and delete entity objects.
def self.dataset
#dataset ||= Google::Cloud::Datastore.new(
project: Rails.application.config.
database_configuration[Rails.env]["dataset_id"]
)
end
# Query Book entities from Cloud Datastore.
#
# returns an array of Book query results and a cursor
# that can be used to query for additional results.
def self.query options = {}
query = Google::Cloud::Datastore::Query.new
query.kind "Post"
query.limit options[:limit] if options[:limit]
query.cursor options[:cursor] if options[:cursor]
results = dataset.run query
posts = results.map {|entity| Book.from_entity entity }
if options[:limit] && results.size == options[:limit]
next_cursor = results.cursor
end
return posts, next_cursor
end
# [START from_entity]
def self.from_entity entity
post = Post.new
post.id = entity.key.id
entity.properties.to_hash.each do |name, value|
post.send "#{name}=", value if post.respond_to? "#{name}="
end
post
end
# [END from_entity]
# [START find]
# Lookup Book by ID. Returns Book or nil.
def self.find id
query = Google::Cloud::Datastore::Key.new "Post", id.to_i
entities = dataset.lookup query
from_entity entities.first if entities.any?
end
# [END find]
# [START validations]
# Add Active Model validation support to Book class.
include ActiveModel::Validations
validates :title, presence: true
# [END validations]
# Save the book to Datastore.
# #return true if valid and saved successfully, otherwise false.
def save
if valid?
entity = to_entity
Post.dataset.save entity
self.id = entity.key.id
true
else
false
end
end
end
routes.rb
Rails.application.routes.draw do
get 'auth/:provider/callback', to: 'sessions#create'
get 'auth/failure', to: redirect('/')
get 'signout', to: 'sessions#destroy', as: 'signout'
resources :sessions, only: [:create, :destroy]
resource :main, only: [:show]
resources :posts
root to: 'posts#index', as: "home"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
EDIT: This is the error log that I'm getting
ActionView::Template::Error (undefined method `to_key' for #<Post:0x2ae2c68>
Did you mean? to_query):
1: <h1>Create Post</h1>
2: <%= form_for #post do |f| %>
3: <% if #post.errors.any? %>
4: <% #post.errors.full_messages.each do |msg| %>
5: <div class="alert alert danger"><%= msg %></div>
app/views/posts/new.html.erb:2:in `_app_views_posts_new_html_erb__241418705_22466964'
Your Post Model is a plain ruby object but you treat it like an ActiveModel/ActiveRecord object.
Try adding
include ActiveModel::Model inside the Post model like so:
class Post
include ActiveModel::Model
...
end
You have defined #posts in new method but you are using #post in the new.html.erb. Which is the reason for the error. Keep the same name either #posts or #post
I'm following a rails tutorial and need some help to proceed further. Problem is, once I fill out the form which has a title,body fields and hit submit, it has to redirect to the show.html.erb page instead it throws an error.
Error: The action 'create' could not be found for PostsController
routes.rb
Rails.application.routes.draw do
get "/pages/about" => "pages#about"
get "/pages/contact" => "pages#contact"
get "/posts" => "posts#index"
post "/posts" => "posts#create"
get "/posts/show" => "posts#show", as: :show
get "/posts/new" => "posts#new"
end
posts_controller_tests.rb
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
def index
end
def new
#post = Post.new
end
def create
#post = Post.new(post_params)
#post.save
redirect_to show_path
end
def show
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
new.html.erb
<h1>Create a new blog post</h1>
<div class="form">
<%= form_for Post.new do |f| %>
<%= f.label :title %>: <br>
<%= f.text_field :title %> <br> <br>
<%= f.label :body %>: <br>
<%= f.text_area :body %> <br> <br>
<%= f.submit %>
<% end %>
</div>
Any help on this would be appreciated.
Note: You are using posts_controller_tests.rb not posts_controller.rb. You are putting your controller code in test controller.
Try to move the code in app/controllers/posts_controller.rb:
class PostsController < ApplicationController
def index
end
def new
#post = Post.new
end
def create
#post = Post.new(post_params)
#post.save
redirect_to show_path
end
def show
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
Your create action always redirects you to the show action. It doesn't matter if your model was saved or not.
You have to check if the model was saved or not:
def create
#post = Post.new(post_params)
if #post.save
flash[:success] = 'Successfully saved'
redirect_to #post
else
render 'new'
end
end
If it wasn't saved, it renders the new action again.
Change your routes.rb to this:
Rails.application.routes.draw do
get "/pages/about" => "pages#about"
get "/pages/contact" => "pages#contact"
resources :posts
end
Moreover you should inherit your controller from ActionController::Base
so change first line of your controller to
class PostsController < ActionController::Base
and move the controller to app/controllers/posts_controller.rb
I have categories nested inside of guides. I'm building an app to learn rails better and I'm trying to make a page that will display all categories that belong to a guide and have edit inputs under them and a save button next to it so the user can edit the names of the categories they want to change.
Bit stuck on how exactly how get this done.
here is the category_item_keys controller
def edit
#guide = Guide.friendly.find(params[:guide_id])
#category = Category.friendly.find(params[:category_id])
#key = #category.category_item_keys
end
def update
#guide = Guide.friendly.find(params[:guide_id])
#category = Category.friendly.find(params[:category_id])
#key = #category.category_item_keys.friendly.find(key_params) # no idea what to make this equal because it isn't one set key being edited on the page
if #key = #category.category_item_keys.update_attributes(key_params)
flash[:success] = "Key updated"
redirect_to #guide
else
render 'edit'
end
end
private
def key_params
params.require(:category_item_key).permit(:key, :slug)
end
routes
match '/guides/:guide_id/:category_id/keys/edit' => 'category_item_keys#edit', :via => :get
match '/guides/:guide_id/:category_id/keys/' => 'category_item_keys#update', :via => :post, as: :category_item_keys_update
edit.html.erb
<ul>
<% #key.each do |key| %>
<li><%= key.key #just shows key name %><br>
<%= form_for([#category, #keys], url: category_item_keys_create_path) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :key, "Key name" %>
<%= f.text_field :key %>
<%= f.submit "Save" %>
<% end %>
</li>
<% end %>
</ul>
This just gives me an error of:
undefined method `to_key' for #<CategoryItemKey::ActiveRecord_Associations_CollectionProxy:0x007fe20a86b480>
Later I plan on using an in-place editor gem but i would like to learn how this can be done fist.
EDIT:
Fixed the error ( changed form_for([#category, #keys] to form_for([#category, key] and turns out this way works for displaying and allowing all categories to be edited... to an extent.
I get another error when i submit a form
undefined method 'update_attribute'
EDIT 2
slowly getting there. I Changed the update #key variable to #key = #category.category_item_keys.all to fix the error. But this line is now giving me problems
if #key = #category.category_item_keys.update_attributes(key_params)'
THIRD EDIT
ERROR
Couldn't find CategoryItemKey without an ID
on line
#key = #category.category_item_keys.find params[:id]
paramaters:
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"egj/OebdSbxxaoaTkr46WVIOIIu4Ezijzu45kqxLT0krjFWHqi67SRJDSgV7bcL6SeoGpUSYsrolspylCXBu9g==",
"category_item_key"=>{"name"=>"def1111"},
"commit"=>"Save",
"guide_id"=>"dbz",
"category_id"=>"characters"}
Here's how to clean up the code:
#config/routes.rb
resources :guides do
resources :categories, path: "" do
resources :category_item_keys, path: "keys", only: [:update] do
get :edit, on: :collection #-> url.com/guides/:guide_id/:category_id/keys/edit
end
end
end
#app/controllers/keys_controller.rb
class KeysController < ApplicationController
def edit
#guide = Guide.find params[:guide_id]
#category = Category.find params[:category_id]
#keys = #category.category_item_keys
end
def update
#guide = Guide.find params[:guide_id]
#category = Category.find params[:category_id]
#key = #category.category_item_keys.find params[:id]
if #key.update key_params
redirect_to #guide, success: "Key Updated"
else
render 'edit'
end
end
private
def key_params
params.require(:category_item_key).permit(:key)
end
end
#app/views/keys/edit.html.erb
<% #keys.each do |key| %>
<%= form_for [#guide, #category, key] do |f| %>
<%= f.text_field :key %>
<%= f.submit %>
<% end %>
<% end %>
If you wanted to use an in-place editor gem, I'd recommend looking at X-Editable, as we've applied it here (its only a demo app, just sign up for free and go to profile):
Looks like you are trying to do update_attributes on a collection instead of an object. Try to first fetch the key object
#key = #category.category_item_keys.friendly.find(params[:id])
and then try to update its attributes
if #key.update_attributes(key_params)
...
end
use nested forms available in rails 4.