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'
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.
I want to be able to show the user that sent out the invitation rather than just my domain when sending out a devise invitation but haven't been able to find any documentation on this.
The two places where I need to show this name are in the invitation email-
(in place of 'Someone')
<p>Hello <%= #resource.email %>!</p>
<p>Someone has invited you to <%= root_url %>, you can accept it through the link below.</p>
<p><%= link_to 'Accept invitation', accept_invitation_url(#resource, :invitation_token => #resource.invitation_token) %></p>
<p>If you don't want to accept the invitation, please ignore this email.<br />
Your account won't be created until you access the link above and set your password.</p>
and the set password page.
<h4>You're seeing this page because someone has invited you to the site</h4>
<%= simple_form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => { :method => :put } do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :invitation_token %>
<div class="row">
<div class="signup_well span3 offset1">
<legend><%= t 'devise.invitations.edit.header' %></legend>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<%= hidden_field_tag :token_key, resource.invitation_token %>
<%= f.submit t("devise.invitations.edit.submit_button") %>
<% end %>
</div>
I may over looking any documentation on the best approach to do this. Your help saves a lot of frustration. Thank you.
This should works: <%= #resource.invited_by.first_name %>.
I have the Following
Page Model
Client Model
Business Model
There is No Relationship between any of these Models.
I would like to Have on the Show Template of the Page to have the Form_for's
One for the Client
One for the Business
Is This Possible?
Current Have The Following:
<div id="sidebar">
<%= form_for (#business) do |f| %>
<div id="contact_form_name">
<p>Company</p>
<%= f.text_field :company_name, :class =>'form_input_small' %>
<%= f.submit 'Submit', :class => 'button' %>
<% end %>
</div>
<div id="sidebar">
<%= form_for (#client) do |f| %>
<div id="contact_form_name">
<p>First Name</p>
<%= f.text_field :first_name, :class =>'form_input_small' %>
<%= f.submit 'Submit', :class => 'button' %>
<% end %>
</div>
Error I am Getting in the Log is the following
<div id="sidebar">
78: <%= form_for (#business) do |f| %>
79: <div id="contact_form_name">
80: <p>Company</p>
81: <%= f.text_field :company_name, :class =>'form_input_small' %>
app/views/pages/show.html.erb:78:in `_app_views_pages_show_html_erb___1556847543_65073939124600'
app/controllers/pages_controller.rb:9:in `show'
Routes
businesses GET /businesses(.:format) {:action=>"index", :controller=>"businesses"}
POST /businesses(.:format) {:action=>"create", :controller=>"businesses"}
new_business GET /businesses/new(.:format) {:action=>"new", :controller=>"businesses"}
edit_business GET /businesses/:id/edit(.:format) {:action=>"edit", :controller=>"businesses"}
business GET /businesses/:id(.:format) {:action=>"show", :controller=>"businesses"}
PUT /businesses/:id(.:format) {:action=>"update", :controller=>"businesses"}
DELETE /businesses/:id(.:format) {:action=>"destroy", :controller=>"businesses"}
Rails doesn't really care where you have your forms so long as you provide it with the necessary information, & there's nothing that says you can't mingle various models together into a single view.
Assuming you're making use of RESTful resources(as you should), you'll have something like:
resources :pages
resources :companies
resources :clients
Setup in your routes.rb, this makes it pretty easy to specify how you want your form_fors to operate.
For instance on your show action for your Page model you could have something like:
<h1>New Company:</h1>
<%= form_for #company, :url => companies_path do |f| %>
...
<% end %>
<h1>New Client:</h1>
<%= form_for #client, :url => clients_path do |f| %>
...
<% end %>
Make sure you're setting the instance variables #company and #client in you pages controller show action like #company = Company.new & #client = Client.new.
In both of these cases your forms will post to the create action of their respective models. You can check out relying on record identification for further reading.
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'