RAILS HABTM checkboxes don't update - ruby-on-rails

I am trying to realize HABTM checkboxes following this tutorial:
http://www.justinball.com/2008/07/03/checkbox-list-in-ruby-on-rails-using-habtm/
While everything seems to work nicely the updates are not saved to my database.
My controller looks like the following:
class UserrolesController < ApplicationController
before_action :set_userrole
def edit
#projects=Project.all
end
def update
params[:userrole][:project_ids] ||= []
#userrole = Userrole.find(params[:id])
if #userrole.update_attributes(userrole_params)
flash[:notice] = "Settings have been saved."
redirect_to edit_userrole_url(#userrole)
else
flash.now[:error] = #userrole.errors
setup_form_values
respond_to do |format|
format.html { render :action => :edit}
end
end
end
private
def set_userrole
#userrole = Userrole.find(params[:id])
end
def userrole_params
params.require(:userrole).permit(:name, :project_ids)
end
end
My _form.html.erb like this:
<%= form_for(#userrole) do |f| %>
<% if #userrole.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#userrole.errors.count, "error") %> prohibited this person from being saved:</h2>
<ul>
<% #userrole.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="center">
<div class="field">
<%= f.label :Name %>
<%= f.text_field :name %>
</div>
<ul class="checkbox-list">
<% #projects.each do |project| -%>
<li><%= check_box_tag "userrole[project_ids][]", project.id, userrole_edits_project?(project) -%> <%= project.name -%></li>
<% end -%>
</ul>
<div class="actions">
<%= f.submit "Speichern", class: "btn btn-primary" %>
</div>
</div>
<% end %>
So I did everything like in the tutorial, the :name is saved without any problems, but the ids are not saved to the database. There is no error message. Does anybody has an idea what might go wrong? Maybe some missing permission somewhere?

So finally I found a work around for this problem.
I forced the update of project_ids by adding the following line in def update:
#userrole.project_ids=params[:userrole][:project_ids]

Related

is missing a template for request formats: text/html

I have Term and Phrase models and I am adding Nested Resources. I want to get the Term Id for Phraes_term to create a data pair
Example: Term 1 and Phrase 2.
But when you press the new button from show.html.erb term, an error will occur
-->error :PhrasesTermsController#new is missing a template for request formats: text/html
form.html.erb
-(phrases_term).
<%= form_with(model: phrases_term, local: true) do |form| %>
<% if pharases_term.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(pharases_term.errors.count, "error") %> prohibited this phrase from being saved:</h2>
<ul>
<% pharases_term.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :ID %>
<%= form.number_field_tag :ID %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
(phrases_term)
-new.html.erb
<%= render 'form', phrases_term: #phrases_term %>
Phrases_terms_controller.rb
class PhrasesTermsController < ApplicationController
before_action :authenticate_user!
before_action :set_term
def new
#phrases_term = PhrasesTerm.new
end
def create
#phrases_term = #term.phrases_term
#phrases_term.user = current_user
#phrases_term.save
redirect_back(fallback_location: root_path)
end
private
def phrases_term_params
params.require(:phrases_term).permit(:term_id)
end
def set_term
#term = Term.find(params[:term_id])
end
end
routes.rb
resources :terms do
resources :phrases_terms, only: [:create, :destroy, :new]
end
(Term)
Show.html.erb
<td><%= link_to 'New', new_term_phrases_term_path(#term) %></td>
(phrases_term).
_form.html.ebr
<%= form_with(model: phrases_term, local: true) do |form| %>
<% if pharases_term.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(pharases_term.errors.count, "error") %> prohibited this phrase from being saved:</h2>
<ul>
<% pharases_term.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :ID %>
<%= form.number_field_tag :ID %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
(phrases_term)
new.html.erb
<%= render 'form', phrases_term: #phrases_term %>
This means you do not have a app/views/phrases_term(s)/new.html.erb file. I'm unsure if you've namespaced the files under 'phrases_term' or 'phrases_terms'.
Make one in the correct directory, populate it, and it will load.

Heroku "Something Went Wrong" when trying to create a new form

I am trying to make a form that will create a new 'room'. However, when I push it to Heroku I am getting this error: 'We're sorry, but something went wrong.' I am using rails 4.1 and ruby 2.1.1. I am also using Heroku.
If it helps, I am kind of new to rails and am trying to port what I learned here to my new 'rooms' type.
This is my view/model/controller:
view: (app/views/rooms/index.html.erb)
<% if current_user %>
<h1>Create Room</h1>
<div class="Sign_Form">
<%= form_for(:room, :url => {:controller => 'rooms', :action => 'create'}) do |f| %>
<p> Created By:</br> <%= f.text_field :created_by%> </p>
<p> Name:</br> <%= f.text_field :name%> </p>
<p> Description:</br> <%= f.text_field :description%> </p>
<%= f.submit :Createroom %>
<% end %>
<% if #room.errors.any? %>
<ul class="Createroom_Errors">
<% for message_error in #room.errors.full_messages %>
<li>* <%= message_error %></li>
<% end %>
</ul>
<% end %>
</div>
<h1>All Rooms</h1>
<% if #rooms.nil? %>
Rooms is nil
<% else %>
<% #rooms.each do |room| %>
<%= room.name %>
<% end %>
<% end %>
<% else %>
<%= render 'welcome' %>
<% end %>
Model: (app/model/room.rb)
class Room < ActiveRecord::Base
class << self
end
end
Controller: (app/controllers/rooms_controller.rb)
class RoomsController < ApplicationController
def index
#rooms = Room.all
end
def new
#room = Room.new
end
def create
#room = Room.new(params[:room])
if #room.save
flash[:notice] = "Room has been created!"
flash[:color] = "valid"
else
flash[:notice] = "Room has not been created!"
flash[:color] = "invalid"
end
render new
end
end
Any help would be highly appreciated. Thanks ahead of time.
EDIT: When I run it on localhost it says: undefined method `errors' for nil:NilClass.

Couldn't find controller without an ID

I'm trying to build Reddit on Rails by Schneems. And I keep getting this error at the point of submitting a new link.
ruby 1.9.3
Rails 4.2.0
What's wrong with my Links#Controller ?
class LinksController < ApplicationController
def show
#link = Link.find(params[:id])
end
def new
#link = Link.new
end
def create
#link = Link.new(links_params)
if #link.save
redirect_to(:action => 'show')
else
render('new')
end
end
private
def links_params
params.require(:link).permit(:title, :url)
end
end
The code should take me to a page showing submitted title and url. But it gives me :
ActiveRecord::RecordNotFound in LinksController#show
Couldn't find Link without an ID
Rails.root: C:/Users/Andrew/Documents/reddit_on_rails
Application Trace | Framework Trace | Full Trace
app/controllers/links_controller.rb:4:in `show'
New view is:
<h1>New link</h1>
<%= form_for #link do |f| %>
<% if #link.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#link.errors.count, "error") %> prohibited this link from being saved:</h2>
<ul>
<% #link.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :url %><br />
<%= f.text_field :url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I've already added strong params to bundler but that did nothing.
I fixed it after four days(finally!).
So links controller should look like this:
def create
#link = Link.new(links_params)
if #link.save
render('show')
else
render('new')
end
Because before it was taking me to page localhost:3000/links/show and I needed localhost:3000/links/id

using partials in Hartl's Rails tutorial, undefined variable method 'object'

I am going through Hartl's Rails tutorial and have been stuck at a rather frustrating point in chapter 10.
<%= form_for(#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 %>
Using this yields this error
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x00000106cad990>:0x00000103395f18>
I have been through it a few times and I am struggling to see where I am going wrong. I've referred to other SO posts but so far none have offered any solution.
Here is
_error_messages.html.erb
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
controller.rb
class MicropostsController < ApplicationController
before_action :signed_in_user
def create
#micropost = current_user.microposts.build(micropost_params)
if #micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
render 'static_pages/home'
end
end
def destroy
end
private
def micropost_params
params.require(:micropost).permit(:content)
end
end
home controller
def home
#micropost = current_user.microposts.build if signed_in?
end
If i am passing 'object' as the argument through the partial it should work no?
Thanks

Undefined method each with nested resources

I'm trying to follow Ryan Bates Polymorphic association tutorial in order to put some comments to my site.
The thing is I have nested resources:
#Nesting Resources
resources :users do
resources :photos do
resources :comments
resources :tags
end
end
So I'm getting an error in my view (photos/show)
undefined method `each' for nil:NilClass
I suppose the problem is in my controller, with comments, that is not defined correctly, but since I have nested resources, I don't know how to do it.
Photos Controller
def show
#photo = Photo.friendly.find(params[:id])
#user = #photo.user
#commentable = #photo
#comments = #commentable.comments
#comment = Comment.new
end
New Comment Partial
<h2>Comments</h2>
<% if #comments.any? %>
<%= render "comments/comments" %>
<% else %>
TodavĂ­a no hay comentarios
<% if user_signed_in? %>
<%= render "comments/form" %>
<% end %>
<% end %>
Form partial (comments/form)
<%= form_for [#commentable, #comment] do |f| %>
<% if #comment.errors.any? %>
<div class="error_messages">
<h2>Please correct the following errors.</h2>
<ul>
<% #comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.text_area :content, rows: 8 %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Comments partial (comments/comments)
<div id="comments">
<% #comments.each do |comment| %>
<div class="comment">
<%= simple_format comment.content %>
</div>
<% end %>
</div>
Your error says undefined method each for nil:NilClass. As you can guess you are calling each on a nil object. Here your #comments is nil and hence giving your trouble.
In your view try something like this:
<div id="comments">
<% if #comments %>
<% #comments.each do |comment| %>
<div class="comment">
<%= simple_format comment.content %>
</div>
<% end %>
<% end %>
</div>
Edit:
So if you look at your code you have #comments till here:
<% if #comments.any? %>
<%= render "comments/comments" %>
<% else %>
#do stuff
<% end %>
it's after you call your partial that your #comment is lost so try this:
<% if #comments.any? %>
<%= render "comments/comments", comments: #comment %>
<% else %>
#do stuff
<% end %>
and then in your view use
<div id="comments">
<% comments.each do |comment| %>
<div class="comment">
<%= simple_format comment.content %>
</div>
<% end %>
</div>

Resources