my routes
TerritoryManagement::Application.routes.draw do
get 'new' => 'territories#new', :as => 'new'
root :to => 'territories#index', :as => 'territories'
resources :territories
resources :users
end
create in my controller
def create
#territory = Territory.new(params[:territory])
if #territory.save
redirect_to root_url, :notice => "Product successfully created!"
else
render "new"
end
my view
<%= form_for(#territory) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
this generates
<form accept-charset="UTF-8" action="/" class="new_territory" id="new_territory" method="post">
I know that this action="/" is the problem, but I don't understand why it's being generated? How to modify my routes that the app will create the record and then goes to the index or edit view?
Thanks
Thomas
This worked for me:
TerritoryManagement::Application.routes.draw do
get 'new' => 'territories#new', :as => 'new'
resources :territories
root :to => 'territories#index'
end
It seems that the :as option was causing some issue. That's there to give the route a name, but since you've already done 'resources :territories' you already have named routes for the standard CRUD actions. I also moved the root route to the end of the file. I can't remember why, but it seems like this was a 'Best Practice' back in the Rails 2.3 days.
Related
I am attempting to create a new user in rails. There is a username, email, password and password confirm field on the new user form. When I click on the create user button on the web page, the new_user form simply refreshes and the user is not added to the database.
Here is the code for my register method in the authentication controller
def register
#user = User.new(params[:user])
if #user.valid?
#user.save
session[:user_id] = #user.id
flash[:notice] = 'Welcome.'
redirect_to sign_in_url
else
render :action => "new_user"
end
end
This is my new_user form:
<p>Sign Up</p>
<%= form_for #user, :as => :user, :url => new_user_path, :method => :put do |f| %>
<p>
<%= f.label 'username:' %><br/>
<%= f.text_field :username %>
<%= show_field_error(#user, :username) %>
</p>
<p>
<%= f.label 'email:' %><br/>
<%= f.text_field :email %>
<%= show_field_error(#user, :email) %>
</p>
<p>
<%= f.label 'password:' %><br/>
<%= f.password_field :password %>
<%= show_field_error(#user, :password) %>
</p>
<p>
<%= f.label 'password confirmation:' %><br/>
<%= f.password_field :password_confirmation %>
<%= show_field_error(#user, :password_confirmation) %>
</p>
<p>
<%= f.submit 'Sign Up' %>
<%= f.submit 'Clear Form', :type => 'reset' %>
</p>
Can anybody point me in the right direction?
Your call to new_user_path in your form references the url to User#new if you have defined resources :users in your routes.rb file. You will need to replace new_user_path with register_user_path, if you have defined register in your routes file. Double check for the path with by running rake routes, though.
From what I can see, there is no need to use have the register action. It'd make more sense to use RESTful actions new, create, index, show, edit, update, and destroy in your case, since your logic follows the intuition behind them. Rails makes this convention easy to follow. See the routing Rails guide here for more.
When the view pass the parameters to controller,
controller gets nil for all of the arguements somehow.
Can anyone how to fix this?? Thanks!
and I have no model called "Message"
controllers/messages_controller.rb
def deliver
recipient = User.find_by_username(params[:recipient])
subject = params[:subject]
body = params[:body]
current_user.send_message(recipient, body, subject)
redirect_to :controller => 'messages', :action => 'received'
flash[:notice] = "message sent!"
end
views/messages/new.html.erb
<%=form_for :messages, url: url_for( :controller => :messages, :action => :deliver ) do |f| %>
<div class="field">
<%= f.label :subject %><br />
<%= f.text_field :subject %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_field :body %>
</div>
<div class="actions">
<%= f.submit %>
<% end %>
Check your source HTML to better understand what FormHelpers do.
With the form_for f.text_field will generate names attributes in the format:
messages[subject]
Consequently, your params will be in the format:
params[:messages][:subject]
You can also use <%= debug params %> to inspect what's in params, it's very helpful.
You can get parameter value using datas = params[:messages]
These values are in array form. So you can fetch array datas If you want to individual data then usesubject = datas[:subject]
body = datas[:body]
To check run following code in view
<%= subject %>
this gives the value of subject.
I am relatively new to programming and to rails so please be indulgent:)
I am building a website for myself which contains a blog. I have two models that are nested and I do not seem to understand how to use REST to perform certain actions on my articles and comments.
When I create a comment if the comment doesn't pass validation I want it to render the page again so that the user can correct his mistakes and resubmit the comment. When I try to render, it gives me a missing template error.
Here is the code:
You can also find this code on github --> https://github.com/MariusLucianPop/mariuslp-
routes.rb
Mariuslp::Application.routes.draw do
get "categories/new"
root :to => "static_pages#index"
match "login" => "visitors#login" # not rest
match "logout" =>"visitors#logout" # not rest
match "comment" => "articles#show"
resources :articles do
resources :comments
end
resources :tags, :taggings, :visitors, :categories, :comments
end
articles_controller.rb
def show
#article = Article.find(params[:id])
#comment = #article.comments.new
end
comments_controller.rb
def create
article_id = params[:comment].delete(:article_id)
#comment = Comment.new(params[:comment])
#comment.article_id = article_id
if #comment.save
redirect_to article_path(#comment.article_id)
else
render article_path(#comment.article_id,#comment) ## This one doesn't work
end
end
def new
#comment = Comment.new
end
def destroy
Comment.find(params[:id]).destroy
redirect_to articles_path()
end
Views-articles:
_comment.html.erb
<div class="comment">
<%= comment.body %><br />
<%= link_to "Delete Comment", article_comment_path(#article), :method => :delete, :confirm => "Are you sure you want to delete this comment?" %>
</div>
_comment_form.html.erb
<%= form_for #comment do |f|%>
<%= f.hidden_field :article_id%>
<%= f.label :body %><br />
<%= f.text_area :body, :cols => 50, :rows => 6 %><br />
<%= f.submit%>
<%end%>
show.html.erb
<p><%= link_to "<< Back to Articles", articles_path%></p>
<div class = "article_show">
<%= label_tag :category_id %>
<%= #article.category_id%> <br />
<%= label_tag :title%>:
<%= #article.title%> <br />
<%= label_tag :body%>:
<%= #article.body%> <br />
<%= label_tag :tag_list%>:
<%= #article.tag_list%><br />
</div>
<br />
<% if session[:username]== "marius"%>
<div class ="admin">
<%= link_to "Edit", edit_article_path(#article)%>
<%= link_to "Delete", article_path(#article), :method => :delete, :confirm => "Are you sure you want to delete this article ?"%>
</div>
<%end%>
<br />
<%= render :partial => 'comment', :collection => #article.comments %>
<%= render :partial => 'comment_form'%>
Have you tried to use where you point the problem?
render 'articles/show'
You don't need to use article_comment_path because that is a full path, not just the place where you store the view templates. In this case, you only need the view. Of couse you must be sure to get all instance variable which you use in this views.
UPDATE:
#article = Articles.find(article_id)
render 'articles/show'
Using this previous question as a guide, I've attempted to create a ul navigation header above a container that renders partials within the container when clicked. (Hopefully that makes some sense, but it may not be important.) Until the links for the partials are clicked, I have it rendering a partial by default.
However, when I went to click my link_to in hopes of rendering the partial I get the following error:
uninitialized constant ProfileController
I'm using Rails 3. Here's my relevant code:
ProfilesController:
def show_about
#is_on_show_about = true
end
def show_info
#is_on_show_info = true
end
views/profiles/show.html.erb:
<div id="info">
<div id="infoContainer">
<% if #is_on_show_about %>
<%= render :partial => 'show_about' %>
<% elsif #is_on_show_info %>
<%= render :partial => 'show_info' %>
<% end %>
<ul id="info">
<li>
<%= link_to 'About', show_about_path, :remote => true %>
</li>
</ul>
<ul id="settingsLinks">
<li>Advice</li>
<li>
<%= link_to 'Info', show_info_path, :remote => true %>
</li>
</ul>
</div>
<%= render :partial => 'show_about' %>
Routes.rb:
map.show_info 'profiles/:id/info', :controller => 'profile', :action => 'show_info'
map.show_about 'profiles/:id/about', :controller => 'profile', :action => 'show_about'
Can anyone help me fix this and explain what went wrong?
Both of your routes are incorrect.
If your controller is indeed named ProfilesController (plural) then your routes should use :controller => 'profiles', instead of :controller => 'profile'.
Given:
The following partial:
<%= form_for #user_session, :url => user_session_path do |f| %>
<%= f.error_messages %>
<span class="field-group">
<div>
<%= f.label :login, "Login Name:" %><br />
<%= f.text_field :login %><br />
</div>
</span>
<div class="clear"></div>
<span class="field-group">
<div>
<%= f.label :password, "Password:" %><br />
<%= f.password_field :password %> <span class="hint">Reminder: Your password is case sensitive.</span><br />
</div>
</span>
<div class="clear"></div>
<span class="field-group">
<div>
<%= f.check_box :remember_me %> <%= f.label :remember_me %>
</div>
</span>
<div class="clear"></div>
<%= f.submit "Login" %>
<% end %>
And the following route:
$ rake routes | grep user_session | grep show
user_session GET /user_session/:id(.:format) {:action=>"show", :controller=>"user_session"}
And the following route configuration:
# user session stuff
resources :user_session do
member do
put :forgot_password
put :terms
get :terms
end
end
match '/login', :to => 'user_sessions#new', :as => 'login'
match '/logout', :to => 'user_sessions#destroy', :as => 'logout'
Problem:
When I call the page that uses this partial, I get the following error:
ActionController::RoutingError in User_sessions#new
Showing /app/views/edit_shared/_login.html.erb where line #2 raised:
No route matches {:action=>"show", :controller=>"user_session"}
Extracted source (around line #2):
1:
2: <%= form_for #user_session, :url => user_session_path do |f| %>
3: <%= f.error_messages %>
4: <span class="field-group">
5: <div>
So that route exists, but rails says it doesn't. So what gives? It was fine until I started upgrading to Rails 3.0.5.
Try removing the :url => user_session_path from your form. By specifying form_for #user_session, Rails will look up the user_session RESTful routes and generate the URL for you.
changing :url => user_session_path to :url => {:action => :create} did the trick.
Your route only accepts GET requests. A form sends a POST request by default.
Edit:
First you should use your named route instead of user_session_path:
<%= form_for #user_session, :url => login_path do |f| %>
And the action you want is create, not new (which displays the form):
match '/login', :to => 'user_sessions#create', :as => 'login'