undefined method `article_path' - ruby-on-rails

this error is occuring and i have no idea why, thanks,
undefined method `article_path' for #<#:0x000001029cb960>
edit.html.erb
<h1>Editing <%= #article.name %></h1>
<%= form_for(#article) do |f| %> <-- ????something here?
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
articles model
class MediaController < ApplicationController
def index
#articles = Articles.all
end
def show
#article = Articles.find(params[:id])
end
def edit
#article = Articles.find(params[:id])
end
def update
#article.find(params[:id])
article_params = params.require(:article).permit(:name, :description, :location)
#article.update(article_params)
redirect_to #article
end
end
routes.rb
resources :media
patch "events/:id" => "media#update", as: "update_medium"

Change your routes to
patch "articles/:id" => "media#update", as: "update_medium"
And model name to singular like :
#articles = Article.all

Related

redirect_to #article doesn't work. article_url not defined

As I said above. I use my code like this.
class ArticlesController < ApplicationController
def show
#article = Article.find(params[:id])
end
def new
end
def create
#article = Article.new(article_params)
#article.save
redirect_to #article
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
But it always doesn't work. I have no idea and other questions are no use.
This is my template.
<%= form_for :article,url: articles_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
My fault I mistyped resources in the the route.
resources :articles

Error adding articles on the page

Following the example guides.rubyonrails.org/getting_started.html I receive an error undefined method `articles' for nil:NilClass in attempt of addition of article on the page.
routes.rb
Rails.application.routes.draw do
root :to => redirect('/pages/1')
resources :articles
resources :pages do
resources :articles
end
views/articles/new.html.erb
<h1>New Article</h1>
<%= form_for([#page, #page.articles.build]) do |f| %>
<p>
<%= f.label :item %><br>
<%= f.text_field :item %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
articles_controller.rb
class ArticlesController < ApplicationController
def new
#article = Article.new
end
def edit
#article = Article.find(params[:id])
end
def create
#page = Page.find(params[:page_id])
#article = #page.articles.create(article_params)
redirect_to root_path if #article.save
end
private
def article_params
params.require(:article).permit(:item, :description)
end
end
What am I doing wrong?
You aren't defining #page in your new action. You need to add something similar to what you've done in create to the new action (and probably the edit action as well).
before_action :load_page
...
protected
def load_page
#page ||= Page.find(params[:page_id])
end

Rails form_for NoMethodError - can't find where broken path is

I try to make my CRM application works and can't figure out where broken part is.
When trying to create new contact, on link '/companies/1/contacts/new'
got 'NoMethodError in Contacts#new'.
Screenshot is attached, see code below. Please help to find mistake..
route.rb is:
Rails.application.routes.draw do
resources :companies do
resources :contacts do
member do
post :new
end
end
end
root 'companies#index'
end
Contacts Controller:
class ContactsController < ApplicationController
before_action :set_company
def index
#contacts = Contact.where(company_id: params[:company_id])
end
def new; #contact = #company.contacts.new; end
def create
#contact = #company.contacts.create(contact_params)
#contact.save ? redirect_to #company : render :new
end
private
def set_company; #company = Company.find(params[:company_id]); end
def contact_params
params.require(:contact).permit(:name, :position, :phone, :email)
end
end
View:
new.html.erb:
<%= render 'form' %>
<%= link_to 'Back', company_contacts_path %>
Form helper:
<%= form_for(#contact) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You need to specify the company as the first argument to form_for:
form_for(#company, #contact)
Then form_for will be able to infer the correct path.

Rails wrong number of arguments (1 for 0) #post.save

I have the following controller :
class PostsController < ApplicationController
def index
end
def new
end
def create
#post = Post.new(post_params)
#post.save
redirect_to #post
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
ANd my model is
class Post < ActiveRecord::Base
end
My new.html.erb page is
<h1> New post </h1>
<%= form_for :post, url:posts_path do |f| %>
<p>
<%= f.label :title %> <br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
routes :
Rails.application.routes.draw do
resources :posts
root "posts#index"
end
I keep getting this error :
ArgumentError in PostsController#create
wrong number of arguments (1 for 0)
what did I do wrong here ?

Rails Ancestry undefined method `model_name' for NilClass:Class

I'm trying to learn how to use the "Ancestry" gem. I watched the related episode in Railscast and get the code. The message table works fine. But I'm trying to duplicate it with a new table called locations. I've copied and changed names on all the code (I believe).
I created the first location. But, when I go to localhost:3000/locations, I get undefined methodmodel_name' for NilClass:Classfor_form.html.erb` line:
<%= form_for #location do |f| %>
routes.rb:
Messenger::Application.routes.draw do
resources :locations
resources :messages
root :to => 'messages#index'
end
_form.html.erb:
<%= form_for #location do |f| %>
<%= f.error_messages %>
<%= f.hidden_field :parent_id %>
<p>
<%= f.label :locname, "New Location" %><br />
<%= f.text_area :locname, :rows => 8 %>
</p>
<p><%= f.submit "Post Location" %></p>
<% end %>
index.html.erb:
<% title "Locations" %>
<%= nested_locations #locations.arrange(:order => :created_at) %>
<%= render "form" %>
locations_controller.rb:
class LocationsController < ApplicationController
def index
#locations = Location.scoped
#location = Location.new
end
def show
#location = Location.find(params[:id])
end
def new
#location = Location.new(:parent_id => params[:parent_id])
end
def create
#location = Location.new(params[:location])
if #location.save
redirect_to locations_url
else
render :new
end
end
def destroy
#location = Location.find(params[:id])
#location.destroy
redirect_to locations_url
end
end
locations_helper.rb:
module LocationsHelper
def nested_locations(locations)
locations.map do |location, sub_locations|
render(location) + content_tag(:div, nested_locations(sub_locations), :class => "nested_locations")
end.join.html_safe
end
end

Resources