undefined method `the_microposts_path' - ruby-on-rails

I'm following the http://ruby.railstutorial.org/
and I'm in chapter10 --build the microposts
but I named the_microposts not microposts
and then I had this problem:
When I visit static_page/home , it shows:
undefined method `the_microposts_path' for #<#:0xb628402c>
and the application trace is:
app/views/shared/_the_micropost_form.html.erb:1:in _app_views_shared__the_micropost_form_html_erb___1052260201__619399208'
app/views/static_pages/home.html.erb:9:in_app_views_static_pages_home_html_erb_733532872_628014758'
It seems that I didn't defined the method?
But I had defined it in my static_pages controller:
def home
#the_micropost = current_user.the_microposts.build if signed_in?
end
My home page:
<section>
<%= render 'shared/the_micropost_form' %>
</section>
and my _the_micropost_form.html.erb is:
<%= form_for(#the_micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new micropost..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
my user models:
has_many :the_microposts, dependent: :destroy
the_micropost models:
belongs_to :user
My routes:
resources :users
resources :sessions, only: [:new, :create, :destroy]
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
is anything wrong??
I always follow the steps, is it because I defined the different name?(the_microposts as microposts)

You need to define route for Micropost resource just like you have defined for users and sessions. Update your config/routes to include resource :microposts.
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :microposts
root 'static_pages#home'
...

Related

pg_search and localization in rails

I am using pg_search and have some trouble with localization.
I have shop_item model that have title1, title2, description1, description2 attributes. Depends from language I am using combination of title1 + description1, or title2 + description2
In application controller i have a method that set locale:
class ApplicationController < ActionController::Base
before_action :set_locale
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
end
ShopItemsController:
def index
if params[:search].present?
#shop_items = ShopItem.search_for_items(params[:search]).paginate(:page => params[:page], :per_page => 32).order('created_at DESC')
else
#shop_items = ShopItem.all.paginate(:page => params[:page], :per_page => 32).order('created_at DESC')
end
end
And in my shop_items/index.html.erb I have:
<%= form_tag shop_items_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search], autofocus: true, type: "text", placeholder: 'search for messages'%>
<%= submit_tag "search", type: "submit"%>
<% end %>
</br>
<% if current_page?(locale: :ua)%>
<div class="row">
<% #shop_items.each do |si| %>
<div class="col m3 ">
<div class="card hoverable">
<div class="card-image">
<%= image_tag si.shop_image.url(:large) %>
</div>
<div class="card-content"-->
<span class="card-title truncate"><td><%= si.title1 %></td></span>
<p><%= t(:price)%>: <%= si.price%></p>
</div>
<div class="card-action">
<%= link_to t(:details), shop_item_path(si) %>
<%= link_to t(:edit), edit_shop_item_path(si) if current_user.present? and current_user.admin? %>
<%= link_to t(:destroy), si, method: :delete, data: { confirm: 'Are you sure?' } if current_user.present? and current_user.admin? %>
</div>
</div>
</div>
<% end %>
</div>
<% end %>
If I am going to shop_items_path I see in my browser next url as default:
http://localhost:3000/shop_items?locale=ua
I can see all my shop_items, but if I am removing locale from url, like:
http://localhost:3000/shop_items
All items are disappear
When I am using search I have url like:
http://localhost:3000/shop_items?utf8=%E2%9C%93&search=item&commit=search
As you can see locale is missing, as the result I can see any shop_items after search.
My question is how to store current locale and include it in search query?
Thanks for any solution!
UPDATED:
Rails.application.routes.draw do
root :to => 'shop_items#index'
resources :shopping_contacts
resources :cart_items
resources :shopping_carts do
#resources :contact_infos
resources :shopping_contacts
resources :cart_items
resources :cart_confirms
end
resources :shop_items do
resources :cart_items
end
resources :contact_us
resources :contacts, only: [:new, :create, :edit, :update, :index, :destroy]
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
resources :users
resources :main_shots
end
Ok thinks for update, maybe you should follow these steps.
1st nest yours routes in 'scope '(:locale)', locale: /en|es/ do:' like that.
Rails.application.routes.draw do
scope '(:locale)', locale: /en|es/ do
root :to => 'shop_items#index'
resources :shopping_contacts
resources :cart_items
resources :shopping_carts do
#resources :contact_infos
resources :shopping_contacts
resources :cart_items
resources :cart_confirms
end
resources :shop_items do
resources :cart_items
end
resources :contact_us
resources :contacts, only: [:new, :create, :edit, :update, :index, :destroy]
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
resources :users
resources :main_shots
end
end
2nd step add this method like that in application_controller.rb:
before_action :set_locale
def set_locale
I18n.locale = params.fetch(:locale, I18n.default_locale).to_sym
end
def default_url_options
{ locale: I18n.locale == I18n.default_locale ? nil : I18n.locale }
end
Last step have you set defautl local in application.rb ?:
config.i18n.default_locale = :en
Give me your feedback, good luck.

undefined local variable or method `your_questions_path'

I receive an error saying that it has an
undefined local variable or method 'your_questions_path'
My routes.rb file:
Rails.application.routes.draw do
get "/" => "main_app#index"
get "/location" => "location#location"
post "/location/index" => "location#index"
get "/location/index" => "location#index"
get "/location/directions" => "location#directions"
root to: 'questions#index'
resources :questions do
collection do
get :your_questions
end
end
get '/logout', to: 'sessions#destroy', via: :delete
resources :users, only: [:new, :create]
resources :sessions, only: [:new, :create]
resources :questions, except: [:new] do
resources :answers, only: [:create]
end
get '/register', to: 'users#new'
get '/login', to: 'sessions#new'
get '/logout', to: 'sessions#destroy', via: :delete
# get '/questions/your_questions', to: 'questions#your_questions' original
get '/questions/:id', to: 'questions#show'
get 'search', to: 'controller#action', as: :search
My view file with the questions path a.k.a app/views/layouts/application.html.erb
<div id="nav">
<ul>
<li>
<%= link_to 'Home', root_path %>
</li>
<% if logged_in? %>
<li>
<%= link_to "Your Q's", your_questions_path %>
</li>
<li>
<%= link_to "Logout (#{current_user.username})", logout_path, method: 'get' %>
</li>
<% else %>
<li>
<%= link_to 'Register', register_path %>
</li>
<li>
<%= link_to 'Login', login_path %>
</li>
I want the questions path to go to http://localhost:3000/questions/1
Thank you in advance!
The routes you've defined create the following routes:
your_questions_questions GET /questions/your_questions(.:format) questions#your_questions
questions GET /questions(.:format) questions#index
POST /questions(.:format) questions#create
new_question GET /questions/new(.:format) questions#new
edit_question GET /questions/:id/edit(.:format) questions#edit
question GET /questions/:id(.:format) questions#show
PATCH /questions/:id(.:format) questions#update
PUT /questions/:id(.:format) questions#update
DELETE /questions/:id(.:format) questions#destroy
The top route is the your_questions route, which means you want use:
your_questions_questions_path
this is happening because you have nested your path inside of a resources block, ie. you have this:
resources :questions do
collection do
get :your_questions
end
end
if you were to get to this path, (run rake routes) you would need to use questions_your_questions_path, however since you want to route to something like http://localhost:3000/quesitons/1 this is a simple 'show' path that is provided to you by the resources command, instead change your routes file to simply say:
resources :questions
and then use question_path(#question) where #question is an instance variable set in the controller.
However... also looking from your code, you seem to be wanting the questions_path instead since that would provide the index action (a listing of questions). You can then in your controller scope the collection of questions to the ones the current user sees...
ie.
QuestionsController < ApplicationController
def index
#questions = current_user.questions
end
(assuming your User has_many :questions)

Couldnt Find Id="your_question"

My error says that:
Couldn't find Question with 'id'=your_questions"
and
ActiveRecord::RecordNotFound in QuestionsController#show
What should I do to fix it?
def show
#question = Question.find(params[:id])
#answer = Answer.new
end
on the second line it says where the error is.
Edit:
The Index View File
<%= form_for(#question) do |f| %>
<%= render 'common/form_errors', object: #question %>
<p>
<%= f.label :body, "Question" %><br />
<%= f.text_field :body %>
<%= f.submit "Ask a Question" %>
</p>
<% end %>
Rails.application.routes.draw do
get "/" => "main_app#index"
get "/location" => "location#location"
post "/location/index" => "location#index"
get "/location/index" => "location#index"
get "/location/directions" => "location#directions"
root to: 'questions#index'
get '/logout', to: 'sessions#destroy', via: :delete
resources :users, only: [:new, :create]
resources :sessions, only: [:new, :create]
resources :questions, except: [:new] do
resources :answers, only: [:create]
end
get '/register', to: 'users#new'
get '/login', to: 'sessions#new'
get '/logout', to: 'sessions#destroy', via: :delete
get '/questions/:id', to: 'questions#your_questions'
get '/search', to: 'questions#search'
You mention you have this route:
get '/questions/your_questions', to: 'questions#your_questions
If you also have a route like following the restful style, you should also have something like:
get 'questions/:id', to: 'questions#your_questions'
Or a resource call. Anyway, so Rails is actually trying to access your show action passing "your_questions" as the id for the route. Write this route like this:
get '/questions/:id', to: 'questions#show
This means: "If a request using the GET HTTP method follows the url 'questions/:id', then go to controller: questions and its action(method in the controller) called: your_questions" and pass into the params hash the value of id as in the URL.

Custom update function routing for rails

I'm implementing a custom update function called update_status for my users controller. I need some help with the routing. what I want to do is update a status that only admins can access. I'm calling the update function via a form helper through the edit function in the users controller. This is my code for the form helper:
<%= form_for #user, :url => url_for(:controller => "users", :action => "update_status"), method: :put do |f| %>
<%= render "shared/error_messages", object: f.object %>
<%= f.check_box :admin %>
<%= f.label :admin %>
<%= f.check_box :editor %>
<%= f.label :editor %>
<%= f.submit "Save Changes", class: "btn btn-primary" %>
<% end %>
But when I click save changes all I get is this error
I want to route the action so that the user id can be resolved.
Controller action code:
def update_status
if #user.update_attributes(status_params)
flash[:success] = "User updated"
redirect_to #user
else
render 'edit'
end
end
Routes:
Transpub::Application.routes.draw do
resources :users do
member do
put 'update_status'
end
end
resources :papers
resources :comments
resources :reviews
resources :sessions, only: [:new, :create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :comments, only: [:create, :destroy]
resources :subject_field, only: [:create, :destroy]
#get "users/new"
root "static_pages#home"
match "/signup", to: "users#new", via: "get"
match "/signin", to: "sessions#new", via: "get"
match "/signout", to: "sessions#destroy", via: "delete"
match "/help", to: "static_pages#help", via: "get"
match "/about", to: "static_pages#about", via: "get"
match "/contact", to: "static_pages#contact", via: "get"
match "/search_papers", to: "papers#index", via: "get"
match "/browse_papers", to: "papers#browse", via: "get"
In your routes files, look for the part that corresponds to the users controller and make sure that you have the following code
resources :users do
put :update_status, on: :member
end
That will declare the route. Another thing you have to update is the url of the form. Change the url to
form_for #user, :url => [:update_status, #user], html: { method: :put } do |f|

Ruby on Rails Form_for error

I'm making a game in Ruby on Rails as a school project but now i'm stuck with this error:
undefined method `storylines_index_path' for #<#:0x007ff34cca8f68>
I'm making an page in which you I wou like to have a form to add an storyline, so I need a form with some fiels in it. I'd like to use the form_for method. But when adding I get this error
Here is my code:
views/new.html.erb
<% provide(:title, 'Overzicht Storylines') %>
<h1>Voeg nieuwe storyline toe</h1>
<%= form_for(#storyline) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :title %>
<%= f.text_field :title%>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
storylines_controller.rb
class StorylinesController < ApplicationController def index
#storylines = Storylines.find(:all) end
def show
#storyline = Storylines.find(params[:id])
end
def new
#storyline = Storylines.new end end
storylines.rb
class Storylines < ActiveRecord::Base
attr_accessible :title, :text
end
routes.rb
StoryLine::Application.routes.draw do
get "users/new"
get "storylines/new"
resources :users
resources :storylines
resources :sessions, only: [:new, :create, :destroy]
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
root to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/contact', to: 'static_pages#contact'
match '/about', to: 'static_pages#contact'
match '/home', to: 'static_pages#home'
end
Rails conventions require that you name your model in a singular form, i.e., Storyline not Storylines. Renaming your model name in the class definition and the controllers should fix this.
When you do
form_for(#storyline)
It will try to look for the storylines_index_path, in order to create a Storyline object into your database.
So you need to define the route on the file config/routes.rb, if you already defined resources :storylines that should define the route, if you don't want to create a REST resource, you can create your own route
match 'storylines/create', to: 'storylines#create', as: :storylines_create
and then on the view
I advise to read the Rails Routing Guide since it explains much better everything related routing on rails

Resources