Afternoon all,
I have a post model, which has a song name, song string fields and a user_id field. A user can create a new song with up to 10,000 characters. Now when saving a short song it displays it all just fine in its entirety, however when a user enters a long song say over 400 chars, it seems to cut most of it off displaying only about 200 chars, the same happens when editing, add the remains of the song and it still cuts it off. Database is Mysql and checking in the console, it doesn’t seem to save the full entry either. The songs are written in keyboard format, e.g E T W R[TREW] | WEER. Ive had a google and haven’t found anything along these lines.
my code is as follows;
post.rb
class Post < ActiveRecord::Base
attr_accessible :song, :song_name, :user_id, :rating
has_many :comments, dependent: :destroy
has_many :ratings, dependent: :destroy
belongs_to :user
before_save :rating
validates_presence_of :user_id
validates :song_name, presence: true, length: { maximum: 70 }
validates :song, presence: true, length: { maximum: 10000 }
default_scope order: "posts.created_at DESC"
posts_controller.rb
def show
@post = Post.find(params[:id])
@comments = @post.comments
@ratings = @post.ratings
respond_to do |format|
format.html
format.json { render json: @post }
end
end
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @post }
end
end
def edit
@post = Post.find(params[:id])
end
def create
@post = current_user.posts.build(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post, success: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new", error: "please sign in to post" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to @post, success: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit", error: "please try again" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
and the show.html.erb
<div class="center hero-unit">
<h1><%= @post.song_name %></h1>
<p>Transcript by: <%= @post.user.name %><br/>
Rating: <%= @post.rating %>/10</p>
<br/>
<p><%= raw @post.song %></p>
<br/>
<span class="timestamp">
updated <%= time_ago_in_words(@post.updated_at) %> ago.
</span>
<%= link_to 'Edit', edit_post_path(@post) %>
<br/>
<% if !signed_in %>
please login to rate songs
<% else %>
<%= render partial: "ratings/form", locals: { post_id: @post.id }%>
<% end %>
</div>
<%= render partial: "comments/form", locals: { post_id: @post.id } %>
<%= render partial: "comments/show", locals: { comments: @comments, post_id: @post.id } %>
if anyone has any ideas it would be much appreciated, and if you need more code just ask. cheers Andy.
It might be that the database field is defined as a varchar which limits the string to 255 characters. But I’m sure you can quickly check that.