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
Related
I am building a method in which users can review other users and I am using https://github.com/mackenziechild/movie_review as a base
views/profiles/show
<%= link_to "Write a Review", new_user_review_path(#user) %>
views/review/._form
<%= form_for([#user, #review]) do |f| %>
<div class="field">
<div id="star-rating"></div>
</div>
<div class="field">
<%= f.label :comment %><br>
<%= f.text_area :comment %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
class Review < ApplicationRecord
belongs_to :user
end
class ReviewController < ApplicationController
before_action :authenticate_user!
def new
#review = Review.new
end
def create
#review = Review.new(review_params)
#review.user_id = current_user.id
#review.receiver_id=#user.id
#review.save
end
private
def set_review
#review = Review.find(params[:id])
end
def review_params
How do I solve this?
Routes
user_review_index_path POST /users/:user_id/review(.:format)
review#create
new_user_review_path GET /users/:user_id/review/new(.:format)
review#new
Routes.rb
resources :users do
resources :review, only: [:new, :create]
end
hmm, maybe I have to do a member do?
Edit, I was playing around with it, I think it's because the model being reviewed is not being set properly in the controller
def set_receiver
#review.receiver = User.find(params[:receiver_id])
end
Adding something like this gives me a "no resource found" error
How do I set the receiver of the review since it's another user?
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
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.
<center><h1> welcome </h1></center>
<%= form_for :authors , url: auth_path do |f| %>[this is the controller auth and model author]
<p>
<%= f.label :title %><br>
<%= f.text_field :topic %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :content ,:cols => "30", :rows => "10" %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
the error is
ActionController::UrlGenerationError in Auth#new
[controller code check it is right or not]
i have tried for it but nothing works
can any one correct the code
class AuthController < ApplicationController
def index
#author = Author.all
end
def show
#author = Author.find(params[:id])
end
def new
end
def create
#author = Author.find(params[:id])
#auth = #author.comments.create(comment_params)
redirect_to #author
end
private
def author_params
params.require(:author).permit(:topic, :content)
end
end
In controller define method same name as this page name in that get #author = Author.find(params[:id]) and in view
<%= form_for #author, :as => :author, :url => auth_path(#author) do |f| %>
And to use single model for multiple controller
class User::AuthorsController < ApplicationController
def welcome
end
end
or
module User
class AuthorsController < ApplicationController
def welcome
end
end
end
You can also call your partials this way
render :template => "user/authors/welcome"
Then in routes.rb
namespace :user do
resources :authors
end
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