I am facing a problem with link_to and yield. I was successfull in showing the root page (start page) in the yield-area of application.erb.html all other links like "help" (see below) are shown on a seperate page.
that is what my application.erb.html looks like:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<%= render 'layouts/navbar' %>
<div class="main">
<div class="main-inner">
<div id="content">
<%= yield %>
</div>
</div>
</div>
<%= render 'layouts/footer' %>
</body>
</html>
In navbar.html.erb I definied two links:
<%= link_to "Start", root_path %>
which works perfect. the second one
<%= link_to "Help", help_path %>
works but is not shown in the yield-area.
routes.rb looks like this:
Rails.application.routes.draw do
get 'set_language/english'
get 'set_language/german'
#scope "(:locale)", :locale => /en|de/ do
# root :to => 'pages#home'
# get "pages/home"
#end
get 'pages/home'
get 'pages/help'
get 'sessions/new'
get 'users/new'
resources :sessions, :only => [:new, :create, :destroy]
root :to => "pages#home"
#root 'pages#home'
get 'help' => 'pages#help'
get 'about' => 'pages#about'
get 'contact' => 'pages#contact'
get 'signup' => 'users#new'
post 'signup' => 'users#create'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
#delete 'logout' => 'sessions#destroy'
get 'logout' => 'sessions#destroy'
I tried using content-for and giving the yield a name identifier but unfortunately without any success.
Related
I've used a form successfully in embedded Ruby that looks like this:
<%= form_for :comment, url: user_comments_path do |c| %>
<%= c.text_field :location %>
<%= c.text_field :title %>
<%= c.text_field :body %>
<%= c.submit ("Submit") %>
<% end %>
However when I tried to put this into Bootstrap I came up against problems. I then tried to make a simpler HTML form like this:
<form accept-charset="UTF-8" action="user/comments" method="post">
<input id="comment_location" name="comment[location]" type="text"/>
<input id="comment_title" name="comment[title]" type="text"/>
<input id="comment_body" name="comment[body]" type="text"/>
<input name="commit" type="submit" value="Create"/>
</form>
...but I came up against the same error message: No route matches [POST] "/users/1/comments/new/user/comments". I am using nested resources, and Rails routes in the terminal tells me that the user_comments path 'POST' equates to user/user_id/comments and the Create controller action.
In my HTML form if I enter action="user/comments" then the resulting path is "/users/1/comments/new/user/comments", if I enter action="comments" the path is "/comments", and if I enter "/" the path is "/". The path that I'm trying to reach is "user/comments" but there seems to be no way there through my HTML form.
I've racked my brains but I can't understand why this might be. Any help would be much appreciated! Thanks
ps. Routes look like this:
Rails.application.routes.draw do
get 'sessions/new'
get 'welcome/index'
root 'welcome#index'
get 'user' => 'users#show'
get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#destroy'
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"
resources :users do
resources :comments
end
resources :sessions
end
update routes.rb use collection instead of resources to get /users/comments like path
resources :users do
collection do
get 'comments'
end
end
I made a new controller to integrate Stripe Checkout, but it's acting as its own entire website and none of my partials (_header, _footer) are displaying.
The current setups is straight from their tutorial https://stripe.com/docs/checkout/guides/rails
Charges Controller
class ChargesController < ApplicationController
def new
end
def create
# Amount in cents
#amount = 100
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken],
:plan => "tier1",
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
Routes
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
get 'charges' => 'charges#new'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :charges
end
/layouts/charges.html.erb (controller view)
<% provide(:title, 'Edit Billing') %>
<%= yield %>
views/charges/new.html.erb (form with Stripe Checkout on it)
<%= form_tag charges_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<label class="amount">
<span>Amount: $1.00</span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="Tier 1 Monthly Subscription"
data-amount="100"
data-locale="auto"
data-image="images/marketplace.png"
data-name="Pallet Tier 1"
data-description="Tier 1 Monthly Subscription"
data-label="Join" >
</script>
views/charges/create.html.erb (redirect after successful charge)
<% provide(:title, 'Edit Billing') %>
<h2>Thanks, you paid <strong>$1.00</strong>!</h2>
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)
I am getting this error: ActionController::UrlGenerationError in ContactUs::Contacts#new, using this gem https://github.com/jdutil/contact_us .
The error: No route matches {:controller=>"contact_us/pages", :action=>"home"}
Where the error is occurring:
.col-xs-8
%ul
%li
= link_to "Home", :controller => 'pages', :action => 'home'
%li
= link_to "About", :controller => 'pages', :action => 'about'
%li
My routes:
devise_for :users
resources :available_times
root :to => "pages#home"
get 'about' => 'pages#about'
get 'pricing' => 'pages#pricing'
get 'users/my-bookings' => 'available_times#index'
get 'users/x34' => 'available_times#create'
get 'users/test_func/:id/:time' => 'available_times#test_func'
Gem routes
Rails.application.routes.draw do
resources :contacts,
:controller => 'contact_us/contacts',
:only => [:new, :create]
get '/contact-us' => 'contact_us/contacts#new', :as => :contact_us
end
Looks like you have a scoping issue. The view is rendered in the contact_us scope and calling a controller in that scope will have it look for it in contact_us/ rather than the entire app. I'm no big fan of using the :controller => 'pages', :action => 'home' for routes generation directly, try and use the path helper instead. It will probably work better.
= link_to "Home", root_path
= link_to "About", about_path
In routes:
get 'about' => 'pages#about', as: :about
I've followed a railscast on creating and displaying static pages using a pages model and my code looks like this:
Pages model
has fields of name, permalink and description.
Routes:
get "log_in" => "sessions#new", :as => "log_in"
get "log_out" => "sessions#destroy", :as => "log_out"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"
resources :pages
match ':permalink', :controller => 'pages', :action => 'show'
_footer.html.erb
<div class="footer">
<div class="footer_container">
<%= link_to 'About Us', root_path('about') %>
</div>
</div>
going to /localhost:3000/about displays the about page correctly but the link in the footer wants to direct to /localhost:3000/.about and actually links to the sign up a new user page.
How can I get my link to direct to /about and display the page?
Thanks for any help its much appreciated!
root_path will always take you to users#new because that is what you specified in your routes.rb file. What you can do is name your last route with the :as key, like this:
match ':permalink', :controller => 'pages', :action => 'show', :as => 'my_page'
Then in your views, you should be able to do something like this:
<%= link_to 'About Us', my_page_path('about') %>